Whats happening to bar
on the second console.log? Shouldn't it be "Andy" or throw a Reference Error? Also, why is foo
not undefined?
Using Chrome.
// lexical scope example
var bar = "Andy";
try {
console.log(bar); // Andy
(function() {
console.log(bar); // undefined!
var bar = "B",
foo = "Last Name";
console.log(bar); // B
console.log(foo); // Last Name
})();
console.log(bar); // B
console.log(foo); // Reference Error
} catch (e) {
console.log(e);
}
JSFiddle of the above: http://jsfiddle.net/2D9fj/3/