I was practicing some scenario and find a case:
Here is fiddle
According to closure bar function should have access to var x
so I expected to alert 1 and condition get false due to if(!1)
but it alerted undefined
and condition get true and second alert is with value 10.
var x = 1;
function bar() {
alert(x);
if (!x) {
var x = 10;
}
alert(x);
}
bar();
So I am confused why it is prompting undefined?
According to hoisting in a particular scope you define a variable anywhere it is considered as defined at top always.
If it is due to hoisting effect it still have to alert 10 instead of undefined.