Global JavaScript Variable Scope: Why doesn’t this work?

What is happening in your first example is the declaration and initlization of msg are being split with the declaration being hoisted to the top of the closure.

var msg; //declaration
msg = "inside" //initialization

Therefore the code you wrote is the same thing as

var msg = 'outside';

function foo() {
var msg;  
alert(msg); // undefined
msg = 'inside';
alert(msg); // inside
}

The second example is not the same. In your second example you have not declared a local variable msg so alert(msg) refers to the global msg.
Here is some further reading on:
Hoisting

Leave a Comment