JavaScript hoisting describes how the engine registers variable and function declarations in memory during the compilation phase before running code line by line. Different declaration keywords behave very differently when referenced early.
flowchart TD
Top["Top of Scope"] --> Func["function fn(): Fully usable above definition"]
Top --> Var["var x: Hoisted and initialized as undefined"]
Top --> LetConst["let / const / class: In Temporal Dead Zone until line executes"]
function greet() {}): Fully hoisted with their body, so calling greet() above its definition works cleanly.var declarations: Hoisted and initialized to undefined until the assignment line runs.let, const, and class: Registered at the top of the block, yet remain uninitialized inside the Temporal Dead Zone until execution reaches their line—reading them earlier throws a ReferenceError.Notice how function expressions (const multiply = ...) follow const TDZ rules rather than function declaration hoisting.
// Works: function declarations are fully hoisted
console.log("Sum:", add(3, 4));
function add(a, b) {
return a + b;
}
// Function expressions and arrow functions are NOT callable before their line
const multiply = (a, b) => a * b;
console.log("Product:", multiply(3, 4));
var initialization values are hoisted: Only the var name; declaration hoists, not = "Ada". Reading name early returns undefined.const fn = () => {} sits in the Temporal Dead Zone until that line executes.No, JavaScript never rewrites your file. During the creation phase before execution, the engine scans the scope and allocates memory slots for declarations first.
class declarations hoisted?Yes, class bindings are hoisted into the Temporal Dead Zone just like let, so instantiating new User() above class User {} throws a ReferenceError.
let and const?Catching early variable reads with an immediate ReferenceError exposes bugs right away instead of letting undefined propagate silently like var did.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.