Glossary / Hoisting
Hoisting
Hoisting is an informal term for declaration instantiation that occurs before ordinary statement execution in a scope. JavaScript does not literally move source text. Different declaration forms are created and initialized differently, which explains why some names are usable before their declaration line while others throw or evaluate as undefined.
Declaration behavior
| Declaration | Binding setup | Before declaration executes |
|---|---|---|
| Function declaration | Function object initialized during instantiation | Usually callable within its scope |
var |
Binding initialized to undefined |
Reads as undefined |
let / const / class |
Binding exists but is uninitialized | Access throws ReferenceError (TDZ) |
Function expression assigned to const |
Function value assigned when expression runs | Binding is in TDZ before initialization |
sayHi(); // works
function sayHi() { return "hi"; }
console.log(count); // undefined
var count = 1;
The table is a mental model, not a source-rewriting algorithm. Duplicate declarations, nested blocks, modules, and function declaration placement have detailed rules. In particular, block-level function declarations in old non-strict scripts had historical differences; modern module code is strict and more predictable.
Engineering implications
Declare values before use even when hoisting permits otherwise. This improves scanability, avoids temporal-dead-zone mistakes, and reduces confusion during refactors. Linters can flag use-before-definition, while tests catch runtime initialization order and module cycles. Hoisting does not mean an assignment is moved upward, and it does not make every declaration immediately usable.
Interview questions
- What does hoisting mean? Declaration instantiation before statement execution, not literal source movement.
- Why does reading a
varbefore its assignment return undefined? Its binding is initialized to undefined before execution reaches the assignment. - Why does reading a
lettoo early throw? Its binding remains uninitialized during the TDZ. - Are function expressions callable before their variable initializes? No; the binding must first receive the function value.