I have been playing around with some pointless logic and scopes and have noticed some strange behavior which has confused me...
var test = 1;
(function(){
console.log(test); //its 1
})();
var test = 1;
(function(){
console.log(test); //its 1
test = 2;
})();
var test = 1;
(function(){
console.log(test); //Uncaught ReferenceError: test is not defined
var test = 2;
})();
In the following examples I would of expected the last function to log out 1 until test is reassigned in that scope however it is undefined, if I remove the scoped declaration and reassign the top level test it then logs out 1 as expected.
Can anyone explain why that last examples test
becomes undefined?