O'Reilly logo
  • Matt Bowen thinks this is interesting:

Function declarations that appear inside of normal blocks typically hoist to the enclosing scope, rather than being conditional as this code implies:

foo(); // "b"

var a = true;
if (a) {
   function foo() { console.log(


Cover of You Don't Know JS: Scope & Closures


Don't try to conditionally declare functions. Just don't.