0

Thought I finally understood hoisting* in JavaScript, but this kind of baffles me. I have noted what I thought is supposed to happen, but can someone explain?

function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = '11';
}

UPDATE June 11th 2016

The following is my understanding/abstraction of "hoisting". of the code above, this is not new code.

function bar() {
    function foo() {} // value function
    var foo;  // value undefined;
    return foo;
    foo = 10;  // I believe this will create a global variable if the function is executed
    foo = '11'; //local foo is assigned the string value 11
 }

 alert(typeof bar()); // returns function

*I understand that "hoisting" is this mental construct we have invented to explain the different phases of compiling via the interpreter.

Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
  • 1
    `var foo` pretty much only reserves the name "foo" in the local scope. `function foo` does the same. In the end they sort of cancel out, the second redundant `var foo` will be ignored. – deceze Jun 10 '16 at 13:56
  • @deceze so essentially when the compiler sees the second `var foo` it's like "sorry ignored", we already have one here, and it's the `function object` Furthermore, if I had declared `var foo = false;` after `var foo = '11'` that also would be ignored and on and on.... – Antonio Pavicevac-Ortiz Jun 11 '16 at 16:48

0 Answers0