Rachel
Rachel

Reputation: 111

Why does x, defined inside a function, become a global variable when I didn't declare it to be a variable in the first place?

Thanks a lot in advance for helping me out!

var f2 = function() {
  x = "inside f2";
};
f2();
console.log(x);
// → inside f2

Why do I get the x as a global variable with value "inside f2" when I didn't declare it to be a global variable with "var x;" before defining the function?

var f2 = function() {
  var x = "inside f2";
};
f2();
console.log(x);
// → Uncaught ReferenceError: x is not defined 

Am I right in assuming that x is not defined in this case because there is no global variable x, only the local variable x within the function f2?

Upvotes: 2

Views: 81

Answers (5)

Felix Kling
Felix Kling

Reputation: 817030

Why do I get the x as a global variable with value "inside f2" when I didn't declare it to be a global variable with "var x;" before defining the function?

Because the specification says so. If you assign to an undeclared variable, a global variable is created. In strict mode this will throw an error (which is more reasonable).

Am I right in assuming that x is not defined in this case because there is no global variable x, only the local variable x within the function f2?

Yes.


8.7.2 PutValue (V, W)
[...]
3. If IsUnresolvableReference(V), then
   a. If IsStrictReference(V) is true, then
     i. Throw ReferenceError exception.
   b. Call the [[Put]] internal method of the global object, passing GetReferencedName(V) for the property name, W for the value, and false for the Throw flag.

Upvotes: 3

Jezen Thomas
Jezen Thomas

Reputation: 13800

When you do x="inside f2" you are indeed declaring a variable. Because there was no x in that function’s scope, the compiler looks outside the current scope for this variable. It doesn’t exist, so it is set as a global.

Doing var x declares the variable in the current scope, which is not necessarily the global scope.

You should use strict; to catch errors like this.

Upvotes: 0

xecgr
xecgr

Reputation: 5193

Declared variables (using var clause) are constrained in the execution context in which they are declared. Undeclared variables are always global.
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Upvotes: 2

kaspermoerch
kaspermoerch

Reputation: 16560

A variable can be declared both with an without the var-keyword. The main differences are that with the var-keyword the variable is contained within the closure in which it is declared - without the variable is considered global.

The reason it becomes global is that the runtime looks up the closure hierarchy to find the given variable. When it doesn't find it, it will declare it in the global scope in order for you to use it.

Upvotes: 0

alex
alex

Reputation: 490527

Doing x = "inside f2" will climb the scope chain until it hits an x or until the global space, where it will do property assignment on the global object.

It doesn't matter if you've declared x in the global space or not.

Upvotes: 0

Related Questions