What object are Javascript global variables stored in?

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.

Answers:

Answer

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 undefined for all three because in that case the code is executed in a frame; so it has a different window object (I think) the code is actually wrapped:

window.addEvent('load', function() {
  var test="stuff";
  console.log(window.test);
  console.log(document.test);
  console.log(this.test);
});

You can see from the above snippet from jsFiddle that test is not a global variable, which explains why it hasn't been attached to window.

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:

<script type="text/javascript">
  var test = "stuff";
  alert(window.test);
</script>

Sure enough, "stuff" every time.

Answer

Here is a late but technical answer.

You ask

Are global variables stored in specific object?

The answer is yes; they are stored in something called, officially, the global object. This object is described in Section 15.1 of the official ECMAScript 5 Specification.

The global object is not required to have a name; but you can refer to its properties, such as String, isNaN, and Date simply by using their name. Your JavaScript host environment will place other properties in the global object besides the ones required by the ECMAScript specification, such as alert or console. In a browser, I can write the script

alert("Hello world");

because alert is a property of the global object.

Note that there does not have to be a way to access this global object at all, believe it or not. The cool thing, however, is that many host environments will put a property in the global object whose value is a reference to the global object itself. In most web browsers, this property is called window. So we can write:

alert("Hello");
window.alert("Hello");
window.window.alert("Hello");
window.window.window.window.window.alert("Hello");

and you can also say:

var x = 5;
alert(this.x);

and get 5 alerted.

Answer

A "true" global variable don't have the "var" keyword. Try this:

test="stuff";
console.log(window.test);
console.log(document.test);
console.log(this.test);

With this, all scopes will see it. Variables with the var keyword are local to the scope that they were declared.

A variable will be "global to window" (property of window object) only if you declare it in the window scope as Dan said, making it global to the browsers that usually use window as the global scope.

Answer

Global variables are stored in the global window variable. The following code works if you just declare it outside anything (like a function):

var test="stuff";
console.log(window.test);

similar proof is that window.location.href is the same as location.href

However, the problem may lie on where the variable was declared. For example, if you declared this variable in the function, it will only exist in the function, and not globally:

function foo(){

    //declaring inside function
    var test="stuff";

    //undefined, since the variable exists in the function only
    console.log(window.test);  

    //undefined, document refers to the document
    //which is the top DOM object, not the global window
    console.log(document.test); 

    //depends on what "this" refers to, especially 
    //when using call() or apply() to call the function
    //For normal function calls, usually it points to window as well
    console.log(this.test);  

    //none of the above refer to "test" that contains "stuff"
    //because you are looking in the wrong place   

}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.