ReferenceError
JavaScript Programming Language
Severity: ModerateWhat Does This Error Mean?
A ReferenceError means you are trying to use a variable that JavaScript cannot find. Either the variable was never declared, it was declared in a different scope, or it was used before it was defined. JavaScript looks for the variable, cannot find it anywhere, and throws this error.
Affected Models
- All browsers
- Node.js
- Deno
- Any JavaScript runtime
Common Causes
- Using a variable name that was never declared with var, let, or const
- Misspelling a variable name — JavaScript is case-sensitive
- Accessing a variable outside the block or function where it was declared
- Using a let or const variable before the line where it is declared
- Forgetting to import a module or library before using it
How to Fix It
-
Look at the exact error message. It will say something like 'myVariable is not defined'. That is the exact name JavaScript cannot find.
Open your browser console with F12 and click the error to see the exact line.
-
Search your code for that variable name. Check for typos — JavaScript is case-sensitive, so 'myVar' and 'myvar' are completely different.
A single wrong capital letter is one of the most common causes of ReferenceError.
-
Make sure the variable is declared before you use it. Move the declaration (let myVar = ...) above the line where you use it.
With let and const, JavaScript does not let you use the variable before it is declared, even if the line is technically in the same file.
-
Check that the variable is in the right scope. A variable declared inside a function cannot be used outside that function.
Think of scope like a room — variables declared inside a room cannot be seen from the hallway.
-
If you are using an external library (like jQuery or Lodash), make sure the script tag or import statement comes before your code that uses it.
If jQuery loads after your code tries to use it, you will get ReferenceError: $ is not defined.
When to Call a Professional
ReferenceErrors are developer errors and are almost always fixable without outside help. If you are working in a large codebase and cannot trace where the variable should come from, ask a senior developer for a code review.
Frequently Asked Questions
What is the difference between 'not defined' and 'undefined' in JavaScript?
These sound similar but mean very different things. 'Not defined' means the variable does not exist at all — you will get a ReferenceError. 'Undefined' means the variable exists but has no value assigned to it yet. For example: let myVar; — myVar exists but its value is undefined. That is normal and will not throw an error.
Why does JavaScript say a variable is not defined when I can clearly see it in my code?
The most common reason is scope. If the variable is declared inside a function or a block (inside curly braces {}), it only exists inside that block. Another common reason is a typo — check the exact spelling and capitalization.
Can I use var to avoid ReferenceErrors?
Using var does avoid some scope-related ReferenceErrors because var is function-scoped and gets 'hoisted'. But this is generally considered a bad practice in modern JavaScript. Instead, organize your code so variables are declared before they are used. This makes your code easier to understand and debug.