JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptHoisting
PrevNext
javascript
beginner
7 min read

Hoisting

const
fundamentals
hoisting
let
scope
temporal-dead-zone
var

Hoisting is JavaScript's behavior of moving declarations to the top of their scope during compilation — var is initialized as undefined, let/const enter a Temporal Dead Zone, and function declarations are fully available before their code position.

Key Points

1var Hoisting

Declaration is hoisted and initialized to undefined; function-scoped, ignores block scope entirely

2let/const and the TDZ

Hoisted but not initialized — accessing before declaration throws ReferenceError due to the Temporal Dead Zone

3Function Declarations

Fully hoisted with name and body — can be called anywhere in scope before the declaration line

4Function Expressions

Follow the hoisting rules of their variable keyword — var gives undefined, let/const gives ReferenceError

5Class Hoisting

Classes behave like let/const — hoisted into the TDZ, cannot be instantiated before declaration

What You'll Learn

  • Know how JavaScript hoisting works for var, let, const, and function declarations
  • Understand the Temporal Dead Zone and why let/const throw ReferenceError before declaration
  • Know the difference between function declaration hoisting and function expression hoisting

Deep Dive

Hoisting is the JavaScript engine's behavior during the compilation phase where variable and function declarations are processed before any code executes. The declarations appear to be "moved" to the top of their containing scope, though the code itself doesn't physically move — the engine simply allocates memory for them first.

var Hoisting

  • var declarations are hoisted and immediately initialized to undefined. This means accessing a var variable before its declaration line returns undefined instead of throwing an error.
  • var is function-scoped (or globally scoped if declared outside a function). It ignores block scope entirely — a var declared inside an if or for block is accessible outside that block.
  • Only the declaration is hoisted, not the assignment. var x = 5 is split into var x (hoisted, initialized to undefined) and x = 5 (stays in place).

let and const Hoisting (Temporal Dead Zone)

  • let and const are technically hoisted — the engine knows about them at the top of their block scope — but they are not initialized. They enter the Temporal Dead Zone (TDZ).
  • The TDZ starts at the beginning of the enclosing block and ends when the declaration line is executed. Any access during the TDZ throws a ReferenceError.
  • This is why let and const are practically described as "not hoisted" — from a developer's perspective, you cannot use them before their declaration.
  • Proof that they are hoisted: if let x inside a block weren't hoisted, console.log(x) before it would read x from an outer scope. Instead, it throws a ReferenceError, proving the inner let x has already "claimed" the identifier.

Function Declaration Hoisting

  • Function declarations are fully hoisted — both the name and the entire function body are available before the declaration line. You can call a function declaration anywhere in its scope, even before it appears in the code.
  • This is unique to function declarations (function foo() {}). Function expressions (const foo = function() {}) and arrow functions (const foo = () => {}) follow the hoisting rules of their variable keyword (var, let, or const).

Function Expressions and Arrow Functions

  • A function expression assigned to var is hoisted as undefined — calling it before assignment throws TypeError: foo is not a function (because undefined is not callable).
  • A function expression assigned to let or const is in the TDZ — calling it before assignment throws ReferenceError.

Class Hoisting

  • Classes are hoisted like let and const — they exist in the TDZ and cannot be used before their declaration.

Key Interview Distinction: undefined vs ReferenceError Accessing a var before its declaration returns undefined (hoisted and initialized). Accessing a let/const before its declaration throws ReferenceError (hoisted but in TDZ). This distinction is one of the most commonly tested hoisting questions in interviews.

Fun Fact

Hoisting was not an intentional language design — it's a side effect of how Brendan Eich implemented JavaScript's two-pass compilation in 10 days. The first pass registers all declarations, the second executes code. When ES6 added let/const, the Temporal Dead Zone was invented specifically to preserve hoisting's scoping behavior while preventing the confusing 'undefined before declaration' problem that var created.

Continue Learning

Variables

beginner

Practice What You Learned

How does JavaScript hoisting work?
junior
hoisting
Hoisting moves variable and function declarations to the top of their scope during compilation. var declarations are initialized as undefined, while let/const enter a Temporal Dead Zone until their declaration is reached.
Previous
Generators & Iterators
Next
Memory Management & Garbage Collection
PrevNext