The following two code snippets produce different outputs, even though the inner function is not being called.
var a = 1;
function foo(){
a= 2;
}
foo();
console.log(a); // 2
However, when I add a function with the same name, the output changes. Even though I am not calling the a()
var a = 1;
function foo(){
a= 2;
function a(){
a = 3;
}
}
foo();
console.log(a); // 1
Shouldn't it be 2? Why does it log 1?