I have a fair bit of understanding about JavaScript scoping -- that the language has function-level scope and the variable and function declarations are hoisted to the top of their containing scope. However, I can't figure out why the following two pieces of code log different values:
This logs the value 1 to the console:
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
console.log(a);
And mysteriously, this logs 10:
var a = 1;
function b() {
a = 10;
return;
}
b();
console.log(a);
So what's going on underneath the hood?