What object are Javascript global variables stored in?

Are global variables stored in specific object? For instance:

var test="stuff";

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 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";

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";

Sure enough, "stuff" every time.


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:


and you can also say:

var x = 5;

and get 5 alerted.


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


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.


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";

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

    //undefined, document refers to the document
    //which is the top DOM object, not the global window

    //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

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



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.