I thought that JavaScript doesn\'t have block scope, but function scope, and that declarations are hoisted from their block to the top of their parent functions.
How
The declaration of inner
is hoisted to the top of the outer function. However, its value is only set if a == 1
.
When outer()
is called with a different value, the call to inner(456)
fails as inner
is still set to undefined
.
if (…) { function …() { … } }
is invalid ECMAScript. Function declarations must be on the top level of function or program bodies, inside of blocks their behaviour is implementation-dependent. Firefox does execute this function statement conditionally, other browsers do hoist it like a normal function declaration. Move it outside the if-statement, or assign a function expression.