function in block (FiB)

a "function declaration" defined in a "block".

JSdeclarationfunctionfunction in block (FiB)

Don't use function (declaration) in block

a function declaration defined in a block.

JS spec says that "function declaration in block is block-scoped"

However, in most browser-based JS engines (including v8):

📗 You Don't Know JS

function (declaration) in block

  • in sloppy mode

    • is hoisted and visible outside the block

    • can be considered as a redeclaration

      • if there's already a function with the same name in the outer scope

      • the (re)assignment is not applied until the block is run

  • in strict mode

    • is local to the block

    • never is a redeclaration any more

👉 compare: var

recommendations for function in block (FiB)

  • don't use it

Last updated