1

I put this code into repl.it expecting to get an undefined is not a function error.

foo();

var foo = function (){
    console.log("Hey foo");
};

Due to hoisting, I thought it would be interpreted as

var foo;
foo();

foo = function (){
    console.log("Hey foo");
};

Instead it logged "Hey foo" to the interactive interpreter.

Does hoisting only apply within the scope of a function or what's happening here?

When I wrapped the above code in a function, as below, the results were as expected, indeed undefined is not a function.

function fooTester = (){
    foo();
    var foo = function (){
        console.log("Hey foo");
    }
}
fooTester();

Just looking for some clarification on what's going on here. repl.it image

4

0 回答 0