I've come across this snippet and I have no idea why it does what it does. Try to answer the question yourself before looking at my answer below the code: what number will this alert?
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {}
}
bar();
alert(foo);
My guess was that this would alert the number 10.. I was wrong. It alerts 1. Why? I'd've thought that "function foo()" won't be even considered, as there's a return keyword before. And even if, it's inside a bar() function scope. I assume that there's some hoisting going on in the background. Can someone explain please?