What hoisting means
Engines create bindings for declarations before running the body of a scope. That is why a function declaration can be called above its line, and why var reads as undefined before its assignment. “Hoisting” is a teaching metaphor for that creation phase — not a source rewrite that literally moves lines of code.
var vs let/const
console.log(a); // undefined var a = 1; console.log(b); // ReferenceError let b = 2;Open in Visualizer
var initializes to undefined during instantiation. let and const are also created early but remain uninitialized until evaluation reaches their declaration — the Temporal Dead Zone (TDZ).
Temporal Dead Zone
From the start of the block until the declaration is evaluated, accessing the binding throws ReferenceError. That includes typeof in some cases for let/const (unlike undeclared globals). Shadowing an outer variable with an inner let puts the name into TDZ for the whole inner block.
const x = "outer";
{
// TDZ for inner x begins here
// console.log(x); // ReferenceError
const x = "inner";
console.log(x);
}Open in VisualizerFunction declarations vs expressions
hoisted(); // works
function hoisted() { console.log("ok"); }
// notHoisted(); // TypeError: not a function
var notHoisted = function () { console.log("nope"); };Open in VisualizerFunction declarations are initialized during instantiation in the same scope. Function expressions assigned to var only get the function value at the assignment line. const fn = () => is TDZ-protected like any const.
When not to rely on hoisting
Readable code declares before use. Relying on function declaration hoisting across large files hides dependency order. Prefer const and modules so initialization order is obvious. Use the visualizer to demo TDZ errors — not as a style guide for production structure.