Monday, 21 August 2017

JavaScript Global Variable with Example



JavaScript Global Variable with Example

A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.

<script>
var value=50;//global variable
function a(){
alert(value);
}
function b(){
alert(value);
}
</script>

Declaring JavaScript global variable within functionFor example:
function m(){
window.value=100;//declaring global variable by window object
}
function n(){
alert(window.value);//accessing global variable from other function
}

Internals of global variable in JavaScript
For example:
var value=50;
function a(){
alert(window.value);//accessing global variable
}

Post a Comment

Whatsapp Button works on Mobile Device only

Start typing and press Enter to search