i have confusion about global variable?? Can You explain with example | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

i have confusion about global variable?? Can You explain with example

Anyone

27th Feb 2019, 10:51 AM
mahaveer pts
mahaveer pts - avatar
2 Answers
27th Feb 2019, 6:12 PM
marjel101
marjel101 - avatar
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
27th Feb 2019, 11:13 AM
wenz
wenz - avatar