On this page

Hoisting and the Temporal Dead Zone

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

To the untrained eye, JavaScript appears to execute code line-by-line from top to bottom. However, the JavaScript engine actually processes your script in two distinct passes: the Creation Phase and the Execution Phase. This internal behavior leads to a phenomenon known as Hoisting.

The Two-Phase Execution​

Before a single line of your code actually runs, the engine scans the script to prepare the environment.

  1. Creation Phase: The engine identifies all variable and function declarations and allocates memory for them. This "hoists" the declarations to the top of their respective scope.
  2. Execution Phase: The engine assigns values to variables and executes the function calls line-by-line.

Hoisting Behavior by Declaration Type​

Not all declarations are hoisted the same way. Understanding these differences is critical for debugging "undefined" values and reference errors.

Function Declarations​

Function declarations are fully hoisted. This means the entire function body is moved to memory during the creation phase, allowing you to call a function before it is defined in the code.


greet(); // Output: "Hello!"



function greet() {

  console.log("Hello!");

}

Var Declarations​

Variables declared with var are hoisted, but only the declaration is moved, not the assignment. The engine initializes them with a default value of undefined.


console.log(user); // Output: undefined

var user = "Alice";

Let and Const Declarations​

Variables declared with let and const are technically hoisted, but they are not initialized. They enter a state called the Temporal Dead Zone (TDZ).

The Temporal Dead Zone (TDZ)​

The TDZ is the period between the start of the block scope and the moment the variable is officially declared. If you attempt to access a let or const variable within this zone, JavaScript will throw a ReferenceError.


{

  // --- Start of Scope ---

  // This is the TDZ for 'price'



  // console.log(price); // ReferenceError: Cannot access 'price' before initialization



  let price = 100; // --- TDZ Ends Here ---



  console.log(price); // Output: 100

}

Why does the TDZ exist?​

The TDZ was introduced in ES6 to encourage better coding habits. It prevents developers from using variables before they are initialized, which was a frequent source of bugs in older var-based JavaScript applications. It ensures that const variables truly remain constant from the moment they are accessible.

Function Expressions vs. Declarations​

A common point of confusion is hoisting in function expressions. Because a function expression is typically assigned to a variable, it follows the hoisting rules of that variable type.


// attempt(); // TypeError: attempt is not a function (it is currently undefined)



var attempt = function () {

  console.log("Testing...");

};

If you used const for the expression above, calling it before the line of definition would result in a ReferenceError due to the TDZ.

Summary Table​

FeatureHoisted?Initialized ValueBehavior when accessed earlyFunction DeclarationYesActual FunctionWorks perfectlyvarYesundefinedReturns undefinedlet / constYes (technically)UninitializedReferenceError (TDZ)

Professional Coding Standards​

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Advanced Engine Concepts

Next

Closures