"ReferenceError" in JavaScript — What It Means and How to Fix It
What the JavaScript error "ReferenceError" means and how to fix it.
Published October 29, 2026
ReferenceError is thrown when code uses a variable that has not been declared in any accessible scope.
What it means and why it happens
Typical causes: typos, scope mistakes (let/const in blocks), missing imports or accessing let/const before declaration (temporal dead zone).
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
ReferenceError: total is not defined
Practical examples
Code that triggers it
console.log(total)
const total = 5The fix
const total = 5
console.log(total)Strict mode helps
'use strict'
undeclared = 1 // ReferenceError instead of creating a globalCommon mistakes
- Missing imports.
- Block scope confusion.
- Using a variable before let/const declaration.
Key takeaways
- ReferenceError: ReferenceError is thrown when code uses a variable that has not been declared in any accessible scope.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
Why 'Cannot access before initialization'?
The let/const binding exists but is in its temporal dead zone.
How do I find which line caused ReferenceError?
The error message and stack trace show the file and line; start there and work backwards.