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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top