ReferenceError: x is not defined
Error message
x is not defined
Quick summary
You referenced a variable name that JavaScript doesn't know about — it was never declared with `let`, `const`, or `var` in any accessible scope.
Why this happens
Typo in the variable name
Forgot to declare the variable with
let,const, orvarThe variable exists in a different scope and isn't accessible here
Minimal example
console.log(userName);const userName = "Alice";
console.log(userName);userName is never declared, so JavaScript throws a ReferenceError — declare it with const before using it.
How to diagnose
Check the spelling of the variable name against its declaration
Verify the variable is declared with
let,const, orvarbefore useCheck the scope — is the declaration visible from where the error occurs?
How to fix
Declare the variable with
constorletbefore using itFix the typo to match the declared name
Move the declaration to a scope accessible from the usage site
How to prevent
Always declare variables with
constorlet— never use bare assignmentsEnable ESLint with the
no-undefrule to catch undeclared variables
Related Resources
Related Lessons
- Variables
Learn how JavaScript variables work
Related Practice
- Let vs Const Quiz
Test your understanding of variable declaration with const.
- Declare a JavaScript Constant
Practice declaring variables with const.