Learn
JavaScript ReferenceError and undefined, explained
Last updated: August 22, 2026
Tell ReferenceError apart from an undefined value. Fix missing names in a free online JavaScript compiler — no account and no Node install required.
See ReferenceError, then declare the nameTwo problems that feel the same
Beginners often say “it is undefined” for every missing-data bug. JavaScript distinguishes a missing name (ReferenceError) from a name that exists and holds undefined.
A ReferenceError means the engine cannot even read the binding. undefined means the binding exists and nothing useful was assigned yet — or a property is missing on an object that does exist.
Output in Code Arena prints the exception type first. Read that word before you start renaming everything in the file.
ReferenceError: the name is not there
Typos, forgotten declarations, and block scope cause most ReferenceErrors. A variable declared with let inside braces is invisible outside. A function parameter is invisible outside that function.
Fix the name or move the declaration up. Do not wrap the line in try/catch and ignore it. That hides the next bug and teaches the wrong lesson.
If you meant a property, use obj.x. A bare x is a different name. People coming from other languages often expect fields to appear as globals.
- Check spelling and capitalization
- Declare the variable before the first use
- Look at braces: is the name trapped in a block?
- If you meant a property, write object.property
The temporal dead zone
let and const exist in a temporal dead zone from the start of the block until their line runs. Reading them earlier is a ReferenceError, not undefined. var is hoisted differently and can surprise you with undefined instead.
In a playground, keep declarations at the top of the snippet while you learn. Once the program works, you can move them next to first use if you want a tighter style.
undefined: the name exists, the value does not
const user = {}; console.log(user.score) does not throw. It prints undefined because score is a missing property, not a missing name.
Calling something that is undefined then becomes TypeError: user.score is not a function. That is a follow-on error. The first fact was the missing property.
Log the object, then the property, then the call. Three small Runs beat one large rewrite.
Practice in the playground
Open the starter snippet. Run it to see ReferenceError: message is not defined. Add const message = "hello" above the log, Run again, then try moving the declaration below the log with let to see the temporal dead zone.
This loop — break, read Output, change one line — is faster than installing Node just to learn scope.
FAQ
- What is a ReferenceError in JavaScript?
- You used a name that does not exist in that scope. The engine cannot even read the value, so it throws before your next line runs.
- Is undefined the same as a ReferenceError?
- No. undefined is a value — the name exists, but nothing useful was assigned. A ReferenceError means the name itself is missing.
- Why does let throw before I assign it?
- let and const are in the temporal dead zone until their line runs. Reading them earlier is a ReferenceError, not undefined.