Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a global var and a window.variable in javascript?

I'm reading the Backbone.js documents and am seeing a lot of code that assigns attributes to the window object:

window.something = "whatever";

What's the difference between calling this code, and just assigning the variable and creating a global variable, like this:

something = "whatever";

I assume there is some kind of scope difference, and/or object ownership difference (window being the owner vs. not), but I am interested in the detail between the two and why I would use window vs. not use it.

like image 489
Derick Bailey Avatar asked Jun 14 '11 19:06

Derick Bailey


People also ask

What is window variable in JavaScript?

window. variableName means that the variable is being declared at the global scope. This means any JS code will have access to this variable. Using window. is not necessary but is frequently used as a convention to denote that a variable is global.

Is window a global variable?

In HTML, the global scope is the window object. All global variables belong to the window object.

What is var global in JavaScript?

The scope of JavaScript variables are either global or local. Global variables are declared OUTSIDE the function and its value is accessible/changeable throughout the program. You should ALWAYS use var to declare your variables (to make locally) else it will install GLOBALLY.

What is the difference between a global variable and a script variable?

A variable's scope is the range of the script where it is visible. Variables have either global or local scope. A global variable exists only once in a script, and is visible in every function. Modifications to it in one function are permanent and visible to all functions.


5 Answers

No difference. They both have the same effect (In the browser, where window is the global context1).

  • window.foo = "bar" sets the property foo on window.
  • foo = "bar" indicates either a typo or intentionally global.

Since I have to double check whether it's a typo or not, I personally find it more readable to set window.foo directly.

Also, in ES5 strict mode, foo = "bar" is an illegal assignment because foo is not declared and will throw a Error.

Edit:

As noted in the comments, foo = "bar" will look all the way up the scope chain for the variable foo and re-assign it with "bar" if it's found. If it's not found, it will create a new global variable.

Also with window.foo = "bar" you're just assigning a property to an object, which can be deleted using delete window.foo.

In ES5 strict mode it is invalid to delete a variable.


1 In other environments, such as node.js and Web Workers, there may be another name for the global object and window may not exist at all. Node.js uses global and Web Workers use self.

like image 189
Raynos Avatar answered Sep 22 '22 18:09

Raynos


They both kind of do the same thing.
But by accessing a window property, you know for sure that you're accessing a global variable no matter what scope you're in.
For example :

globalVar = "smth"; function(){     var globalVar = 2;     alert(globalVar);// points to the current scope globalVar     alert(window.globalVar);// points to the original globalVar } 

In other words, If you want to work with globals, it's somewhat safer to access them via their container : window.variable

like image 26
gion_13 Avatar answered Sep 22 '22 18:09

gion_13


The key, as Raynos alluded to, is that it's set explicitly on the window object. In the browser, the global object is the same as the window object but in other environments (e.g., Node.js, or perhaps running in a web view of some sort on a mobile device), it may not.

like image 20
ScottKoon Avatar answered Sep 20 '22 18:09

ScottKoon


The difference is that window.foo = bar; cannot be intercepted by refactoring done later. Using foo = bar; means that if, at a later date, the code is moved into a closure where var foo has been defined, it will no longer set it on the global object.

like image 26
david Avatar answered Sep 18 '22 18:09

david


Adding one more point:

If you refer an undeclared variable directly (without using - window or typeof) then you will get a variable is not defined error.

Examples:

// var unDecVariable

if (unDecVariable != null) // Error: unDecVariable is not defined
{
    // do something
}

if (window.unDecVariable != null) // No Error
{
    // do something
}

if (typeof unDecVariable != 'undefined' && unDecVariable != null) // Alternative way
{
    // do something
}
like image 45
SridharKritha Avatar answered Sep 21 '22 18:09

SridharKritha