patternjavascriptCritical
What is the purpose of the var keyword and when should I use it (or omit it)?
Viewed 0 times
varomitshouldandusethepurposewhenwhatkeyword
Problem
NOTE: This question was asked from the viewpoint of ECMAScript version 3 or 5. The answers might become outdated with the introduction of new features in the release of ECMAScript 6.
What exactly is the function of the
and
?
When would you use either one, and why/what does it do?
What exactly is the function of the
var keyword in JavaScript, and what is the difference betweenvar someNumber = 2;
var someFunction = function() { doSomething; }
var someObject = { }
var someObject.someProperty = 5;and
someNumber = 2;
someFunction = function() { doSomething; }
someObject = { }
someObject.someProperty = 5;?
When would you use either one, and why/what does it do?
Solution
If you're in the global scope then there's not much difference. Read Kangax's answer for explanation
If you're in a function then
If you're not doing an assignment then you need to use
If you're in a function then
var will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it):// These are both globals
var foo = 1;
bar = 2;
function()
{
var foo = 1; // Local
bar = 2; // Global
// Execute an anonymous function
(function()
{
var wibble = 1; // Local
foo = 2; // Inherits from scope above (creating a closure)
moo = 3; // Global
}())
}If you're not doing an assignment then you need to use
var:var x; // Declare xCode Snippets
// These are both globals
var foo = 1;
bar = 2;
function()
{
var foo = 1; // Local
bar = 2; // Global
// Execute an anonymous function
(function()
{
var wibble = 1; // Local
foo = 2; // Inherits from scope above (creating a closure)
moo = 3; // Global
}())
}var x; // Declare xContext
Stack Overflow Q#1470488, score: 1413
Revisions (0)
No revisions yet.