Skip to content
jshacks.dev
Fundamentals

The Temporal Dead Zone in JavaScript Explained With Examples

The temporal dead zone is the time between entering a scope and running a let, const or class line, when reading the name throws. See where it hides.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, Vite, Node.js and TypeScript logos.

ReferenceError: Cannot access 'config' before initialization is one of the clearest error messages in JavaScript and still one of the more puzzling, because the variable is plainly declared a few lines further down. The message is telling you something precise: the name exists, and its value has not been set yet. The stretch of execution where that is true has a name, the temporal dead zone, and understanding it takes about five minutes and removes a category of confusion for good. This post defines the zone, shows the places it appears, including default parameters and circular imports, and explains why the language bothers. Examples run on Node.js 24 as ES modules. It is the other half of the post on hoisting, and it is the reason let and const behave differently from var in the comparison of the three keywords.

The zone, in one example

Here the access happens before the declaration line, in the same block.

JavaScript
{
  try {
    console.log(price);
  } catch (error) {
    console.log(error.name + ": " + error.message);
  }

  const price = 25;
  console.log(price);
}
Text
ReferenceError: Cannot access 'price' before initialization
25

From the opening brace until the const price = 25 line executes, the binding is in its dead zone. The first read throws. After the declaration runs, the binding is initialized and reads work. Notice that the error is a ReferenceError, not a TypeError and not undefined: the language refuses to hand out a value that does not exist.

It is about time, not position

The word "temporal" is the important one. A function written above a const in the file can use that const without trouble, as long as the call happens after the declaration has run.

JavaScript
function readConfig() {
  return config.retries;
}

try {
  readConfig();
} catch (error) {
  console.log(error.message);
}

const config = { retries: 3 };

console.log(readConfig());
Text
Cannot access 'config' before initialization
3

The function body mentions config above the declaration, which is fine. The first call runs too early and throws, and the second call runs after the line and succeeds. Nothing about the function changed: the difference is whether execution has reached the declaration yet. This is why a module can define functions at the top that use constants defined below, and it works as long as nothing calls them in between.

typeof does not save you

The typeof operator is the safe way to probe for a name that was never declared, because it returns "undefined" instead of throwing. That safety does not extend to a name in its dead zone.

JavaScript
console.log(typeof neverDeclared);

try {
  console.log(typeof declaredLater);
} catch (error) {
  console.log(error.name);
}

let declaredLater = 1;
Text
undefined
ReferenceError

The first line is the old guarantee and still holds. The second fails, because the binding exists. A name that does not exist is not the same as a name that is not ready, and the language treats them differently on purpose. The post on typeof lists this as the one case where the operator can throw.

Default parameters have a zone

Parameters are initialized from left to right, and a default value expression can see only the parameters before it. A default that refers to a later parameter reads one still in its dead zone.

JavaScript
function range(start = end - 10, end = 100) {
  return [start, end];
}

try {
  range();
} catch (error) {
  console.log(error.name + ": " + error.message);
}

console.log(range(5));

try {
  range(undefined, 50);
} catch (error) {
  console.log(error.name + ": " + error.message);
}

function fine(start = 0, end = start + 10) {
  return [start, end];
}

console.log(fine());
Text
ReferenceError: Cannot access 'end' before initialization
[ 5, 100 ]
ReferenceError: Cannot access 'end' before initialization
[ 0, 10 ]

Calling range() with no arguments tries to compute start from end, which has not been initialized. Providing start skips its default, so no early read happens. The third call is the surprising one: passing end does not help when start still needs its default, because start is evaluated first, while end is still in its dead zone, whatever the caller supplied. And the fine version orders the parameters so each default only looks backward, which is the rule to follow. The same left-to-right rule applies to destructuring with defaults.

A name in its own initializer

A declaration is in its dead zone while its own initializer runs. Referring to the name on the right-hand side reads it before it is ready.

JavaScript
const label = "outer";

function build() {
  try {
    const label = label + "!";
    return label;
  } catch (error) {
    return error.message;
  }
}

console.log(build());

const factorial = (n) => (n <= 1 ? 1 : n * factorial(n - 1));
console.log(factorial(5));
Text
Cannot access 'label' before initialization
120

The inner label hides the outer one for the entire block, so the initializer reads the inner one and fails. The factorial example looks similar and works, because the reference to factorial sits inside a function body that runs later, after the constant has been initialized. The initializer only creates the arrow function, and does not call it.

Classes

A class declaration has a dead zone too, which is why you cannot instantiate a class above its definition. Inside the class body, however, the class name is available from the start, because the body is evaluated with an inner binding.

JavaScript
try {
  new Invoice();
} catch (error) {
  console.log(error.message);
}

class Invoice {
  static create() {
    return new Invoice();
  }
}

console.log(Invoice.create() instanceof Invoice);
Text
Cannot access 'Invoice' before initialization
true

The first call throws because the class statement has not run. After it, the static method can use its own class name freely. The usual consequence is ordering: define a class before the code that instantiates it, and a lint rule will tell you when you forget.

Circular imports

The place a dead zone is hard to see is a pair of modules that import each other. ES modules are evaluated in dependency order, and in a cycle one module runs before the other has finished. If it reads an exported const at the top level, it reads the binding in its dead zone.

JavaScript
// a.mjs
import { b } from "./b.mjs";
export const a = "A";
console.log("a sees b =", b);

// b.mjs
import { a } from "./a.mjs";
export const b = "B";
console.log("b sees a =", a);

Run from a.mjs, module b.mjs is evaluated first, and its top-level read of a throws ReferenceError: Cannot access 'a' before initialization, because a.mjs has not yet reached export const a. The fix is to move the read into a function that runs later, to break the cycle by extracting shared code into a third module, or to read the value lazily. Function declarations are exported with their body already initialized, so they escape the problem, which is one reason shared helpers are often declarations. The post on ES modules explains live bindings and evaluation order.

Why the language has it

var gave JavaScript twenty years of bugs in which a variable was read before it was set and quietly returned undefined. When let and const arrived, the committee had two choices: make them behave like var and start as undefined, or fail loudly. For const there was a stronger reason to fail. A constant that reads as undefined and then changes to its real value would not be constant. The dead zone guarantees that no code can ever observe a const with a value other than the one it was declared with.

The cost is the error message above and a slightly stricter mental model. The benefit is that a whole class of ordering mistakes becomes an exception at the exact line, instead of a wrong value found later.

Where the zone gets in the way

Teams run into it when they reorganize a file and move a const below its first use, or when two modules start importing each other during a refactor. The error message points at the right line, and the usual fix is a reorder. If you find yourself reaching for var to make the error go away, that is the signal to fix the order instead, since the var only hides the same mistake as undefined.

For a deliberately lazy value, such as a singleton created on first use, a function that initializes on demand is a clearer tool than a let declared early and assigned late.

The behavior is defined in the specification under let and const declarations, and MDN covers it on its let reference page.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.