In the following code:
function outer() {
var x = 'foo';
function inner() {
var y = x; // y == 'foo'
var x = 'bar'; // x == 'bar', y == undefined
}
}
Why does the variable y
become undefined within inner()
? shouldn't it refer to x
in outer()
?
If the line var x = 'bar';
is removed then y
does indeed have the value 'foo'.