Javascript Global Variable Is Actually A Property Of Window Object

javascript global variabe

Posted by Taher Chhabra on Thursday, September 15, 2011 Tags: Design Pattern   1 minute read

I was reading about JQuery and came to know something about Javascript global variables. A Javascript glabal variable is declared as follows

var myGlobalVar = 5; function myFunction() alert(myGlobalVar); // 5 }

The above code will give you result 5 Now use the code given below

var myGlobalVar = 5; function myFunction() alert(window.myGlobalVar); // 5 }

Voila !! The Global variable became the property of the window object.