I don't understand why the value of foo
is undefined in the following code:
var foo = 1;
function bar() {
if (false) {
var foo = 10;
}
alert("Foo is " + foo);
}
bar();
Result : Foo is undefined
.
https://jsfiddle.net/yk7ae9b0/
Whereas these variations behave as expected.
var foo = 1;
function bar() {
if (true) {
var foo = 10;
}
alert("Foo is " + foo);
}
bar();
Result : Foo is 10
var foo = 1;
function bar() {
if (false) {
// var foo = 10;
}
alert("Foo is " + foo);
}
bar();
Result: Foo is 1
Any help will be really appreciated.