How to Fix "ReferenceError: x is not defined" in JavaScript
Why "ReferenceError: x is not defined" happens — an identifier that was never declared in a reachable scope — and the six fixes, from typos and scope to the temporal dead zone.
ReferenceError: x is not defined means the identifier you referenced was never declared in any scope reachable from that line, so the engine cannot resolve it to a binding. Unlike undefined — a declared variable with no value — "not defined" means the name itself does not exist.
What the error means across browsers
Chrome and Node say Uncaught ReferenceError: x is not defined; Firefox says the same; Safari says Can't find variable: x. A close cousin — Cannot access 'x' before initialization — is the temporal dead zone, covered below.
The 6 common causes and their fixes
1. A typo or wrong casing in the name
Identifiers are case-sensitive. userName and username are different names.
const userName = "Ada";
// ❌ different name
console.log(username);
// ✅
console.log(userName);
2. Using a variable outside the block it was declared in
let and const are block-scoped — they do not exist outside their { }.
// ❌ total only exists inside the if block
if (true) { const total = 5; }
console.log(total);
// ✅ declare in the outer scope
let total;
if (true) { total = 5; }
console.log(total);
3. A missing import or library that never loaded
Referencing something from a module you did not import throws.
// ❌ never imported
const id = uuidv4();
// ✅
import { v4 as uuidv4 } from "uuid";
const id = uuidv4();
4. The temporal dead zone — using let/const before its line
let and const are hoisted but not initialized until their declaration runs; touching them earlier throws Cannot access 'x' before initialization.
// ❌ ReferenceError: Cannot access 'count' before initialization
console.log(count);
let count = 3;
// ✅ declare before use
let count = 3;
console.log(count);
5. Referencing a name that was never declared (strict mode)
Reading a bare name that has no declaration anywhere throws — add the declaration.
"use strict";
// ❌ no `let score` anywhere
console.log(score);
// ✅
let score = 0;
console.log(score);
6. A runtime mismatch (Node vs browser globals)
Using a global that does not exist in that environment — window in Node, or document in a worker — throws. Use the runtime-appropriate global.
// ❌ window is not defined in Node
console.log(window.location);
// ✅ use the universal global
console.log(globalThis);
Quick diagnostic checklist
- Check spelling and exact casing against the declaration.
- Confirm the variable is declared in a scope that encloses the usage — not a nested block or function.
- If the reference sits above a
let/const, it is the temporal dead zone — move the declaration up. - Verify the identifier is imported and the module actually loaded, and that the global exists in your runtime.
Frequently asked questions
What is the difference between "not defined" and "undefined"?
undefined means the variable exists but has no value assigned — that does not throw. If you see ReferenceError, the fix is a declaration or import, not a value.Why do I get "Cannot access x before initialization"?
let and const are hoisted but stay uninitialized until their declaration line executes, so using them earlier throws. Move the usage below the declaration.How do I safely check if a variable exists?
typeof x !== "undefined". It is the one operation that does not throw on an undeclared identifier, which makes it the standard guard for optional globals.Test any of these fixes instantly in XCODX Studio — it runs JavaScript in the browser with a live console, so you can watch a ReferenceError disappear the moment you add the declaration or import.