BugHunt

ReferenceError: Cannot access before initialization

You used a let or const before its declaration line executed. This is the temporal dead zone.

JavaScriptScope and closure bugs

What it means

let and const are hoisted like var, but they stay unusable until their declaration runs. Touching one before that is a ReferenceError rather than undefined — deliberately, because it turns a silent bug into a loud one. The error names the variable, which makes it one of the friendlier failures once you recognise the phrase.

Common causes

1. Declaring after use

The lines read top to bottom; the constant does not exist yet on the earlier line.

Breaks

const total = base + fee;
const fee = 5;

Works

const fee = 5;
const total = base + fee;

2. Shadowing an outer name

The inner declaration covers the whole block, so the outer value is unreachable within it.

Breaks

const label = "outer";
function show() {
  console.log(label);
  const label = "inner";
}

Works

const label = "outer";
function show() {
  console.log(label);
  const innerLabel = "inner";
}

How to find it in your own code

Declare before use, always. If the error names a variable that clearly exists above, look for a second declaration of the same name inside the current block — that shadow is the culprit.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other common errors