0
i have confusion about global variable?? Can You explain with example
Anyone
2 Respostas
0
A global variable is accessible from everywhere in the code.
This wouldn't be the cas if you define a variable inside a function.
// example for global var
var myvar1 = 'bla';
function foo1() {
console.log(myvar1);
}
foo1(); // prints 'bla'
// example local variable
function foo2() {
var myvar2 = 'bla';
console.log(myvar2);
}
foo2(); // prints 'bla'
BUT:
If you access myvar2 outside of foo2, it won't work:
console.log(myvar2); // doesn't print anything