ReferenceError: x is not defined in JavaScript
A ReferenceError means a variable does not exist. Here is what causes it and how to fix each case.
ReferenceError: x is not defined in JavaScript
A ReferenceError is thrown when you try to use a variable that does not exist in any accessible scope. The message is "x is not defined," which is confusingly close to undefined.
Common Causes
1. Using a Variable That Was Never Declared
console.log(foo); // ReferenceError: foo is not defined
Fix: Declare it first.
2. Misspelling a Variable Name
const userName = "Kunal"; console.log(userNam); // ReferenceError: userNam is not defined
Fix: Fix the typo.
3. Using a Variable Outside Its Scope
function foo() { const inner = 5; } console.log(inner); // ReferenceError: inner is not defined
Fix: Declare at the right scope, or return the value.
4. TDZ Access for let/const
console.log(x); // ReferenceError: Cannot access 'x' before initialization let x = 5;
The message is different ("Cannot access 'x' before initialization"), but it is a ReferenceError. Fix: Declare before use.
5. Forgetting to Import
// module.js console.log(utils.formatDate()); // ReferenceError: utils is not defined
Fix: import utils from "./utils.js".
The Confusing Message
ReferenceError: x is not defined sounds like "x is undefined," but they are different:
undefinedis a value (the variable exists but has no value).not definedmeans the variable does not exist at all.
Safe Existence Check
typeof does not throw for undeclared variables:
if (typeof someGlobal !== "undefined") { someGlobal.doSomething(); }
This is the safe pattern for checking optional globals (e.g., feature detection).
The Takeaway
A ReferenceError: x is not defined means x does not exist in any accessible scope. Common causes: undeclared variables, typos, scope errors, missing imports, and TDZ access. Use typeof for safe existence checks.
It means the variable x does not exist in any accessible scope. It was never declared, is misspelled, is out of scope, or was not imported. It is different from undefined, which means the variable exists but has no value.
Because it sounds like 'undefined,' but they are different. undefined is a value for a declared-but-empty variable. 'not defined' is an error meaning the variable does not exist at all.
Use typeof. typeof an undeclared variable returns 'undefined' without throwing. For example: if (typeof someGlobal !== 'undefined') { ... } is safe for feature detection.
Accessing a let or const variable before its declaration line throws ReferenceError: Cannot access 'x' before initialization. The message is different from 'not defined' but it is still a ReferenceError.
Using an undeclared variable, misspelling a variable name, using a variable outside its scope, accessing a let/const in the TDZ, and forgetting to import a module.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

