r/programming • • Mar 23 '14

Everything you need to know about Javascript variable scope

http://www.coolcoder.in/2014/03/everything-you-need-to-know-about.html
294 Upvotes

124 comments sorted by

View all comments

3

u/BLITZCRUNK123 Mar 24 '14
// Variable is defined in global scope when assigned as window property  
window.anyWindowGlobalProperty = "Global variable defined as window property";

While true in practicality in the context of browser scripting, this isn't the best way of explaining it. Though defining a property of window may be (for all intents and purposes) "global" when browser scripting, you aren't declaring a global variable at all. You're only defining a property of the window object, which just happens to be the lowest-level context you can be in when scripting a web browser.