To declare a global variable, use the var at global scope −
<script>
var myGlobalVariable;
function display() {
//
}
</script>For JavaScript function, assign the property to a window −
<script>
function display() {
window. myGlobalVariable = …;
}
</script>