While most script engines support function declarations within blocks, it is not part of ECMAScript 5 and below, and from browser to browser the implementations are inconsistent with each other. ECMAScript 5 and below only allow function declarations in the root statement list of a script or function. If you are targeting browsers that don't support ECMAScript 6, use a variable initialized with a function expression to define a function within a block :

Noncompliant Code Example

if (x) {
  function foo() {}
}

Compliant Solution

if (x) {
  var foo = function() {}
}