Search code examples
javascripthoisting

does this variable get hoisted no matter what?


Does var foo get hoisted to the top of the stack even when the code inside the false block isn't ever going to be executed?

function foo(){
    if ( false ) {
       var foo = 'bar'; //will this be hoisted even if its never executed?
    }
}

I'm seeing that it is and was just confused...I didn't expect it to get hoisted in its wrapped in a false condition.


Solution

  • Yes; the hoisting happens before the code is run, so whether or not the if statement comes out true or false isn't yet known.