ReferenceError
In JavaScript, ReferenceError is the native error type that the engine throws when code uses a variable name that it can’t resolve, or one that isn’t initialized yet. It extends Error, so it carries a message, a name, and a stack trace. Reading a name that no scope declares is enough to throw it:
> const total = subtotal * 1.2;
Uncaught ReferenceError: subtotal is not defined
When JavaScript Throws ReferenceError
The engine throws ReferenceError at run time, when the line that uses the name executes, not when it parses the code. These are the usual situations, most common first:
- Reading a name that no scope declares, because of a typo, a missing
import, or a browser global such asdocumentin Node.js - Reading a
let,const, orclassbinding in its temporal dead zone, before its declaration runs - Assigning to an undeclared name in strict mode, where sloppy mode would create a global variable instead
- Using
thisin a subclass constructor before callingsuper() - Assigning to a function call, such as
getUser() = data
The typeof operator reads an undeclared name without an error and returns "undefined". It still throws for a binding in its temporal dead zone. The constructor takes the same arguments as Error, though code seldom throws a ReferenceError on purpose.
Catching ReferenceError
Say a script calls a helper that a refactor renamed. A catch block can check the type with instanceof and rethrow anything else:
> try {
… applyDiscount(42);
… } catch (error) {
… if (!(error instanceof ReferenceError)) throw error;
… error.message;
… }
'applyDiscount is not defined'
The instanceof check fails for errors from another realm, such as an iframe or a node:vm context, while error.name still reads "ReferenceError". In most code, declaring, importing, or renaming the binding fixes the cause, so the catch block is seldom needed.
Common Messages
Node.js 26 and Chrome print these ReferenceError messages, most common first:
| Message (V8) | Usual Cause | Fix |
|---|---|---|
x is not defined |
A typo, a missing declaration or import, a browser global such as window in Node.js, or a strict-mode assignment to an undeclared name |
Fix the spelling, declare or import the name, or guard a host global with typeof x === "undefined" |
Cannot access 'x' before initialization |
A let, const, or class binding used in its temporal dead zone, often through a circular import |
Move the declaration above its first use, or break the import cycle |
require is not defined in ES module scope, you can use import instead |
Node.js: require() called in an ES module |
Use import, or createRequire() from node:module |
__dirname is not defined in ES module scope |
Node.js: a CommonJS variable, also __filename, module, or exports, used in an ES module |
Use import.meta.dirname and import.meta.filename, or export |
Must call super constructor in derived class before accessing 'this' or returning from derived constructor |
A subclass constructor that reads this before super(), or never calls it |
Call super() before the first use of this |
Invalid left-hand side in assignment |
An assignment to a function call, such as getUser() = data |
Assign to a variable or a property instead |
Firefox and Safari word some of these messages differently:
| V8 | Firefox (SpiderMonkey) | Safari (JavaScriptCore) |
|---|---|---|
x is not defined |
x is not defined |
Can't find variable: x |
Cannot access 'x' before initialization |
can't access lexical declaration 'x' before initialization |
Cannot access 'x' before initialization. |
Invalid left-hand side in assignment |
cannot assign to function call |
Left side of assignment is not a reference. |
For a strict-mode assignment to an undeclared name, Firefox prints assignment to undeclared variable x instead. Older Safari releases print Cannot access uninitialized variable. for the temporal dead zone, and MDN still lists that wording. Most x is not defined errors go away once the name is spelled the way it’s declared, or imported where it’s used.
Related Resources
Course
JavaScript Errors and Debugging for Pythonistas
Port a food bank's donation import to JavaScript, and learn to catch, throw, and tell apart errors, check for the bad data JavaScript doesn't throw on, and find what went wrong.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026