admin管理员组

文章数量:1397058

I'm studying variable scope in Javascript, and have e across the difference between variable declaration, and variable initialization. From talking to a developer I know, my understanding is that writing var before a variable declaration assigns the variable to the local scope, while not writing var before declaring the variable assigns the variable to the global scope. Is this true?

If writing var before declaring a variable does assign the variable to the local scope, is it necessary to write var later, when initializing the variable to keep it in the local scope? For example:

var someVariable;
// Do some things with JavaScript
someVariable = 'Some Value'

Since I declared someVariable in the local scope with var, but then initialized someVariable without using var, does JavaScript think that I just initialized one variable in the local scope, or that I declared one variable in the local scope, and then declared and initialized another variable in the global scope?

Later on, when I want to change the value of someVariable again, do I need to write var before the variable expression, or will JavaScript know that I'm changing the value of an already declared local variable? Technically speaking, how does JavaScript know when I'm changing the value of an already declared local variable, and when I'm declaring and initializing a global variable?

I'm studying variable scope in Javascript, and have e across the difference between variable declaration, and variable initialization. From talking to a developer I know, my understanding is that writing var before a variable declaration assigns the variable to the local scope, while not writing var before declaring the variable assigns the variable to the global scope. Is this true?

If writing var before declaring a variable does assign the variable to the local scope, is it necessary to write var later, when initializing the variable to keep it in the local scope? For example:

var someVariable;
// Do some things with JavaScript
someVariable = 'Some Value'

Since I declared someVariable in the local scope with var, but then initialized someVariable without using var, does JavaScript think that I just initialized one variable in the local scope, or that I declared one variable in the local scope, and then declared and initialized another variable in the global scope?

Later on, when I want to change the value of someVariable again, do I need to write var before the variable expression, or will JavaScript know that I'm changing the value of an already declared local variable? Technically speaking, how does JavaScript know when I'm changing the value of an already declared local variable, and when I'm declaring and initializing a global variable?

Share Improve this question asked Feb 27, 2013 at 20:40 Code JunkieCode Junkie 1,3823 gold badges14 silver badges24 bronze badges 1
  • I'd also add that I'm currently working on improving my understanding of the correct terms to use when talking about JavaScript. As such, I wouldn't be upset by being corrected on how I use terms. – Code Junkie Commented Feb 27, 2013 at 20:57
Add a ment  | 

4 Answers 4

Reset to default 3
var something = "Initial value."

This means: "create a variable in the local scope and give it an initial value". The local scope means the function in which you use this statement.

something = "New value."

This means: "find variable 'something' in the nearest scope, and assign it a new value". If you use the second statement without ever using the first, the statement will look for any definition of something in progressively bigger scopes (the function that contains your function, if it exists, the function that contains that, etc., until it reaches the global scope). If it finds something, it will assign to an already existing variable. If it finds nothing, it will create a global variable with that name.

If you use var first, you simply ensure that this search always stops at local scope.

These are the same:

var x;
// ...
x = 1;

...and...

var x = 1;

Both define a variable in the local scope and assign a value to it. If you want to change the value of the variable later in the same scope you can simply reference it by name:

x = 2;

If you're in a different scope however, unless the variable was declared in the global scope in the first place you will not be able to access it (it's "out of scope"). Attempting to do so will define a variable with that name in the global scope.

function a(){
    var x = 1;
}

function b(){
    x = 2;  // 'x' in a is out of scope, doing this declares a new 'x' in global scope
}

a();
b();

When referencing a variable in the same scope it was declared, you do not need to prefix it with var, though you can:

var x = 1;
// ...
var x = 2;

...there's no need to do that. While it assigns 2 to 'x', it logically has no effect since var is already in local scope. If x had been declared globally however:

var x = 1;

function a(){
    var x = 2;
    console.log(x);
}

a();
console.log(x);

This will print first '2' and then '1'. By referencing x preceded with var in the function, it applies the local scope to the variable. Once the function pletes the variable's original scope is restored (or the re-scope is lost, if you want to look at it that way). Thanks to @zzzzBov for pointing this out.

Hope this helps.

my understanding is that writing var before a variable declaration assigns the variable to the local scope, while not writing var before declaring the variable assigns the variable to the global scope. Is this true?

Not entirely.

function foo() {
    var a = 1;
    function bar() {
        a = 2; // Still in the scope of foo, not a global
    }
}

Since I declared someVariable in the local scope with var, but then initialized someVariable without using var, does JavaScript think that I just initialized one variable in the local scope, or that I declared one variable in the local scope, and then declared and initialized another variable in the global scope?

There is only one someVariable in that example.

Later on, when I want to change the value of someVariable again, do I need to write var before the variable expression

var scopes a variable for the entire function, no matter where in the function it appears.

If you define a variable using var, you can refer to the same variable without using the var keyword over and over. These refer to the same variable:

var someVariable;
//...code...
someVariable = 'rawr';

If you did use the var keyword every time you were changing the variable, you wouldn't get separate variables. The newest declaration would just overwrite the oldest declaration. So there's no point in using the var keyword except for initialization. To change the value of someVariable, you can just make assignments to the variable name like in the above example.

Basically, using var will create a new variable if there is no variable in that scope with the same name.

Now take this code for example:

var someVariable = 'initialized';

function test1(){
  //this someVariable will be a new variable since we have the var keyword and its in a different scope
  var someVariable = 'test1';
  console.log(someVariable);
}

function test2(){
  //because there is no var keyword this refers to the someVariable in the parent scope
  someVariable = 'test2';
  console.log(someVariable);
}

console.log(someVariable); //initialized

test1(); //test1
console.log(someVariable); //initialized

test2(); //test2
console.log(someVariable); //test2

With this example you can see that depending on what you want the code to do, you could be having problems. If you wanted test2 to act like test1 and forgot to use the var keyword you would be confused when you were expecting someVariable to be initialized and instead it was test2.

But you could have also purposely not used the var keyword because you wanted test2 to update the parent variable. So it is important that you use the var keyword correctly.

Not using var when initializing variables will create the variable on the global scope. This is not good practice. If you want variables on the global scope, manually put them there. i.e. window.someVariable = 'initialize'; That way anyone else that sees your code knows that you made it a global variable on purpose.

本文标签: Initializing a JavaScript variable in the local scopeStack Overflow