BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutSync devicesRSSGitHubX
Posts/javascript
AI-generated illustration. One name is already in hand. The other stays behind the rope until its line runs.
Beginner5 min read

Part 6 of JavaScript Plot Twists

Hoisting and the TDZ: Why let Throws and var Is undefined

var and let are both registered when the scope starts. var is already undefined. let stays untouchable until its line runs.

Mohsen Fallahnejad

Mohsen Fallahnejad

Nov 16, 2026Discuss
js
console.log(a); // undefined
var a = 1;
js
console.log(b); // ReferenceError
let b = 1;

Both names exist before those lines. Only one of them lets you touch it.

The mental model: a rope in front of the name#

When a scope starts, JavaScript registers every name declared inside it. The assignments stay where you wrote them. Nothing is moved to the top of the file.

var is registered as undefined, so reading it early is safe and useless. let is registered too, but it sits behind a rope until its line runs. Touching it early throws.

That rope is the temporal dead zone. Temporal means time: from the moment the scope starts until the declaration line runs.

AI-generated illustration. The name behind the rope already exists. Touching it before its line runs is what throws.AI-generated illustration. The name behind the rope already exists. Touching it before its line runs is what throws.

What a scope does with a name

  1. 1

    Enter the scope

  2. 2

    Register every declared name

then it depends
var
  1. ↳Already undefined
  2. →Reading it returns undefined
let or const
  1. ↳Behind the rope
  2. →Reading it throws until its line runs
The assignment never moves. Only the registration happens early.

How it works#

Hoisting is that early registration. People picture the engine cutting the var line and pasting it at the top. It doesn't. The name is created up front. The = 1 stays put.

js
function demo() {
  console.log(x); // undefined
  if (false) {
    var x = 1; // this assignment never runs
  }
}

x becomes undefined when demo starts, even though the var sits in an if that never runs. var is function-scoped: one registration for the whole function, ignoring the block.

let and const are block-scoped, and they start with no value:

js
{
  console.log(y); // ReferenceError
  let y = 1;      // the rope drops here
}

Before that line, y has a name and nothing you may read. Reading it, writing it, even typeof y, throws ReferenceError. After the line, y is 1. const uses the same rope. Its line must set the value, and you can't assign a new one later.

typeof is the proof that the name is real and still forbidden:

js
typeof missing; // "undefined"  — there is no such binding
typeof later;   // ReferenceError — the binding exists, the rope is up
let later = 1;

A name that was never declared is a quiet "undefined". A let you haven't reached yet is an error, because the scope already knows the name.

In practice#

Declare let and const before any line in that block that reads them. If the value isn't ready, compute it first, then declare.

Prefer them to var. The throw is the useful part. var handing back undefined lets a bug travel, and you meet it later as a wrong value.

Honest stakes: you'll hit the rope when one block reads a const declared further down, or when an inner let reuses a name from outside. Declaring at the top of the block means you won't see it in ordinary code. Interviews ask anyway, because the two answers look almost the same until you say when the value appears.

Say it in an interview#

"var and let are both hoisted: the name is registered when the scope starts, and the assignment stays on its line. var is initialized to undefined, so reading it early gives undefined. let and const stay uninitialized until their line runs. That gap is the temporal dead zone, and touching the name throws ReferenceError. typeof on a missing name is undefined, but typeof on a let still in the zone throws, because the name exists."

Cheat sheet#

varlet and const
Registered whenThe function startsThe block starts
Before its lineundefinedNo value. Don't touch it
Read it earlyundefinedReferenceError
ScopeThe whole functionThe block
typeof before its line"undefined"ReferenceError

TL;DR#

  • The name is registered when the scope starts. The assignment stays on its line.
  • var starts as undefined. Reading it early does not throw.
  • let and const stay behind the rope until their line runs. Touching them throws ReferenceError.
  • typeof on a name that doesn't exist is "undefined". typeof on a let still behind the rope throws.

Advanced · optional

Going deeper#

Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.

An inner let hides the outer one for the whole block#

js
let n = 1;
{
  console.log(n); // ReferenceError, not 1
  let n = 2;
}

The inner let n is the name for every line of that block, including the lines above it. The outer n is hidden, and the inner one is still behind the rope. Same spelling, two bindings. The inner one owns the block from the first line.

A function declaration arrives already filled in#

js
greet(); // "hi"
function greet() {
  return "hi";
}

wave(); // TypeError: wave is not a function
var wave = function () {};

A function declaration is registered and given its value when the scope starts, so a call above the line works. var wave = function () {} only hoists the name wave, as undefined. The function value arrives on the assignment line. Calling undefined throws TypeError, which is a different error from the dead zone's ReferenceError.

class uses the same rope#

A class declaration is hoisted like let, not like function. The name exists for the whole scope, and using it before its line throws ReferenceError.

The rope shows up across modules too#

ES module bindings are live. In a circular import, module A can see module B's export before B has run the line that sets it. Reading that export throws the same ReferenceError. The binding exists. Its line hasn't run.

Further reading#

  • Hoisting, MDN
  • let and the temporal dead zone, MDN
Filed under#javascript#hoisting#tdz#let#var#interview

Series

JavaScript Plot Twists

Real bugs caused by JavaScript doing exactly what the spec says — and nothing you expected.

  1. 1The Leaderboard That Ranked 9 Points Above 100
  2. 2You Selected Rows 3, 8, 12, 15 and 20. JavaScript Checked 3, 1 and 8.
  3. 3JavaScript ASI: Why return on Its Own Line Gives undefined
  4. 4The $4.35 Item That Was Charged $4.34
  5. 5The Leaderboard Arrived Sorted. JavaScript Re-sorted It by User ID.
  6. 6Hoisting and the TDZ: Why let Throws and var Is undefined · you are here
  7. 7The birthday that showed up a day early · coming soon
  8. 8The regex that matched every other time · coming soon

New parts land here first — subscribe below to get them in your inbox.

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderHow Garbage Collection Works: It Never Looks for Unused MemoryNewer →Barrel Files (`index.ts`): Why One Import Can Slow Your Build

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

How Garbage Collection Works: It Never Looks for Unused Memory

The collector paints every object reachable from the stack and globals. Whatever the paint misses is unused, even if objects still point at each other.

Nov 16, 20267 min read

You Selected Rows 3, 8, 12, 15 and 20. JavaScript Checked 3, 1 and 8.

Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3]: map passes the index as parseInt's radix. Plus the Number('') trap hiding in the obvious fix.

Nov 1, 20264 min read

The Leaderboard That Ranked 9 Points Above 100

Why JavaScript's sort() compares numbers as strings, why a 'lazy' comparator works in Safari but breaks in Chrome, and the one-line fix.

Oct 31, 20265 min read

Get new posts in your inbox

One email when a new post is out: short, diagram-first explanations of JavaScript, React and Next.js. No spam, unsubscribe any time.

5 min left
Discuss👍 React
👍