In an interview, I was asked to guess the output of the following snippet:
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {
}
}
bar();
console.log(foo);
and I thought the output will be 10 because the bar function returns right after reassigning the value of foo but the actual output is 1. Is it because of the function definition with the same variable name(foo)? Is a new scope created for the foo
variable inside bar
?