I wanted to learn more about when to use function expressions vs function declaration in JavaScript and stumbled across this article which has the following code snippet:
function foo(){
function bar() {
return 3;
}
return bar();
function bar() {
return 8;
}
}
alert(foo());
To my surprise, and because of hoisting, this evaluates to 8
. When a declaration gets hoisted, does it get moved all the way to the top of the function (it would be hoisted before the first bar() that returns 3) or, would it be hoisted after all other function declarations (it would be hoisted after the first bar() that returns 3)?