1
var in_window = 'a' in window;
alert(in_window);
var a = 1;
//a = 1;

If I use var to declare a, then in_window will be true. However, If I don't use var to declare a, then in_window will be false.

What exactly the difference between using var and not using var here?

This code is not inside of a function. In my opinion ,I think a is a global variable whether using var or not. But why in_window's value are not same.

huashui
  • 1,758
  • 2
  • 16
  • 24
  • Is that code inside of a function? – Thilo May 02 '13 at 02:15
  • This code is not inside of a function. In my opinion ,I think a is a global variable whether using var or not. But why in_window's value are not same. – huashui May 02 '13 at 02:19
  • The result you describe seems backwards. If you use `var`, then `in_window` will be `true` unless you're running the code inside a function. If you don't use `var`, then `in_window` will be `false` because the assignment is taking place after the `in` test. –  May 02 '13 at 02:19
  • ...since you're not in a function, the result you describe is definitely backwards. –  May 02 '13 at 02:21
  • [With `var` result is `true`](http://jsfiddle.net/QKNgp/) ...but... [Without `var` result is `false`](http://jsfiddle.net/QKNgp/1/) –  May 02 '13 at 02:23
  • 2
    So the reason is that `var` does what people call "hoisting", by which they mean that the `var` declaration *(but not the value assignment)* is moved to the top of the variable environment, so it takes place before your `in` test. –  May 02 '13 at 02:26

2 Answers2

0

Essentially, if you use var, it creates a variable in your local scope. If you do not, it creates it in global scope. Also, if you use var, it "hoists" the definition to the top of a function. For example:

var a = 1;

function b() {
   alert(a); //alerts 1 (global)
   a = 2; //global is now 2
   alert(a); //alerts 2
}

function c() {
   alert(a); //alerts undefined, var a got hoisted to the top
   var a = 3; //local = 3
   alert(a); //alerts 3
}

b();
c();
alert(a); //alerts 2 (global);
dave
  • 62,300
  • 5
  • 72
  • 93
  • This code is not inside of a function. In my opinion ,I think a is a global variable whether using var or not. But why in_window's value are not same. – huashui May 02 '13 at 02:20
0

Every variable in Javascript exists in a context. When you do not use var to declare it, it gets put into the top-level context, which in the usual case of a browser is the window object. In node.js, running on the server, it will be that system's top-level context.

So, with the var a = 1 line, a is put into the its function's context. With the a = 1 line, it goes in the window, causinga in window` to be true. I hope you didn't use quotes there in practice.

There is one thing you missed here; the Javascript parser reads through each function to find all the variable and function declarations before it actually interprets the function. The order of the two statements in the function is irrelevant:

var f = function() {
    alert(a in window);
    var a = 0;
};
Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
  • Not completely irrelevant. The variable will be defined at the top, but the assignment takes place later (where the line that does it is). – Thilo May 02 '13 at 02:30
  • True. It's rare to refer to a variable without its value, but that's what his test checked. We should use JSLint to help us keep to standards. – Eric Jablow May 02 '13 at 03:02