Lesson

JS Hoisting

10 min reading
Free Course

JavaScript Hoisting: Declaration Behavior Before Execution

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.

Hoisting Rules by Keyword

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 declarations (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.

Practical Hoisting Comparison Example

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));

Common Mistakes to Avoid

  • Assuming var initialization values are hoisted: Only the var name; declaration hoists, not = "Ada". Reading name early returns undefined.
  • Calling arrow functions before declaring them: Even though arrow functions look like short functions, const fn = () => {} sits in the Temporal Dead Zone until that line executes.
  • Relying on hoisting to bury helper functions carelessly: Define variables at the top of blocks, and keep related helpers clearly grouped.

Frequently Asked Questions

Does JavaScript physically move lines of code to the top of the file?

No, JavaScript never rewrites your file. During the creation phase before execution, the engine scans the scope and allocates memory slots for declarations first.

Are JavaScript 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.

Why did ES6 introduce the Temporal Dead Zone for let and const?

Catching early variable reads with an immediate ReferenceError exposes bugs right away instead of letting undefined propagate silently like var did.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
JavaScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum