Guides/Hoisting and the Temporal Dead Zone

Hoisting and the Temporal Dead Zone

Declarations are processed before evaluation — but initialization timing differs by keyword.

Rajan Pandey · About the author

Rajan Pandey is a Full Stack AI Engineer with nearly 3 years of professional experience building production-ready web applications, SaaS platforms, and developer-focused tools. He specializes in JavaScript, TypeScript, React.js, Next.js, Node.js, and the MERN stack.He has worked on 15+ production applications across SaaS, EdTech, and GenAI products, with experience spanning frontend architecture, backend development, REST APIs, database design, authentication, and third-party integrations. StackTools was created as a way to make everyday development tasks simpler and JavaScript concepts easier to understand through interactive, browser- based tools.His goal is to build practical utilities that developers can use without unnecessary complexity, registration, or uploading their data to external servers.

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 Visualizer

Function declarations vs expressions

hoisted(); // works
function hoisted() { console.log("ok"); }

// notHoisted(); // TypeError: not a function
var notHoisted = function () { console.log("nope"); };
Open in Visualizer

Function 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.

Try it live

Open the matching StackTools utility — runs in your browser.

Open JS Execution Visualizer