Are global variables stored in specific object? For instance:
var test="stuff";
console.log(window.test);
console.log(document.test);
console.log(this.test);
All three of these tests result in undefined, so is there an object that holds these variables?
I feel as though this is something stupid that I should already know, but I can’t even seem to find the answer online.
I think you’ll find on most browsers, they are stored in
window.Far-fetched psychic debugging attempt: did you test this in jsFiddle? Or perhaps in Firebug? If so, you’re probably seeing
undefinedfor all three because in that casethe code is executed in a frame; so it has a differentthe code is actually wrapped:windowobject (I think)You can see from the above snippet from jsFiddle that
testis not a global variable, which explains why it hasn’t been attached towindow.I’m no expert, but this answer appears to be accurate from what I can tell in Chrome, Firefox, Safari, and Opera. To verify, I created an HTML file with the following content and loaded it in each browser:
Sure enough, “stuff” every time.