CKTIS / JavaScript
Function declaration hoisting in JavaScript
A `function` declaration can be called before it appears in source code. A function expression used as an initializer does not make its function available the same way.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
function name() {
// Function body
}Function declaration hoisting means JavaScript initializes the name’s binding with the function object before executing statements in the relevant scope. When the name resolves to that binding, a call can appear before the declaration. No source code is moved, and the function body runs only when called.
A declaration directly in a function body is set up on each call to the enclosing function. At the top level of a classic script, if global declaration instantiation succeeds, the function’s binding is backed by a property of the global object, typically accessible through globalThis. If the global object already has a same-named own property that is non-configurable and non-writable, instantiation can throw TypeError before any statement in that script runs. In a module, the binding is initialized during linking, stays module-scoped, and does not create a globalThis property. Hoisting does not make a declaration in a later script available before that script is processed.
In strict mode, including in modules, a declaration inside a block is initialized when the block is entered. You can call it before its declaration within that block, but the binding is not visible outside. In non-strict code, Annex B’s separate web compatibility rules can add an outer binding. These rules are required in web browsers and optional in other hosts. Do not rely on that binding in portable code.
A function expression or arrow function creates its function only when the expression is evaluated. In var fromVar = function () {}, the fresh local binding starts as undefined, so trying to call it early throws TypeError. With let or const, the binding is in the temporal dead zone (TDZ) until initialized, so reading it or trying to call it early throws ReferenceError.
Examples
(function () {
"use strict";
console.log(declared());
function declared() { return "declared"; }
try {
fromVar();
} catch (error) {
console.log(error.name);
}
var fromVar = function () {};
try {
fromConst();
} catch (error) {
console.log(error.name);
}
const fromConst = () => {};
{
console.log(blockOnly());
function blockOnly() { return "block"; }
}
})();Result
declared TypeError ReferenceError block
Run the whole snippet at once. `declared()` and `blockOnly()` work before their declarations in their respective scopes. Both errors are caught so the example can finish.
Related lessons
- JavaScript · Lesson 5 · Advanced · Version: 4
- JavaScript · Lesson 8 · Advanced · Version: 4
- JavaScript · Lesson 16 · Advanced · Version: 4
Sources
- ECMAScript 2024 §15.2 — Function Definitions
- ECMAScript 2024 §10.2.11 — FunctionDeclarationInstantiation
- ECMAScript 2024 §16.1.7 — GlobalDeclarationInstantiation
- ECMAScript 2024 §16.2.1.6.4 — InitializeEnvironment
- ECMAScript 2024 §14.2.3 — BlockDeclarationInstantiation
- ECMAScript 2024 Annex B.3.2 — Block-Level Function Declarations Web Legacy Compatibility Semantics
- HTML Standard — The script element
- MDN — function declaration
- MDN — function expression
- MDN — Hoisting