This is javascript hoisting behavior. Take a look at this link. In your case, the script is interpreted like this:
var foo = 1;
function bar() {
function foo() {}
foo = 10;
return;
}
bar();
console.log(foo);
Because:
Function declarations and variable declarations are always moved
(“hoisted”) invisibly to the top of their containing scope by the
JavaScript interpreter
In your case, when you assign foo = 10;
, you're assigning to the local variable. That's why the global variable is still unchanged.