Error Reference › JavaScript
JavaScript
X is not defined (ReferenceError)
You used a name that doesn't exist in scope.
What it looks like
Uncaught ReferenceError: $ is not defined
at main.js:3:1
What does "X is not defined (ReferenceError)" mean?
Thrown when code uses a name that does not exist anywhere in scope. The classic case is a library such as jQuery being used before its script tag has loaded, or a variable declared inside a function being used outside it.
What causes "X is not defined (ReferenceError)"?
- A typo in a variable/function name.
- Using a variable before it's declared.
- A missing import or script not loaded.
- Wrong scope (defined inside another function/block).
How do I confirm the cause?
- Search the codebase for where the name is declared — it may not exist at all, or be spelled differently.
- For a library global, check the script tag order and whether a defer, async or failed download means it is not loaded yet.
- For let and const, check the variable is not being used above its declaration in the same block.
How do I fix "X is not defined (ReferenceError)"?
- Check the spelling of the name.
- Make sure it's declared/imported and the script is loaded first.
- Move the declaration above its use.
How do I stop it happening again?
- Use modules and explicit imports instead of relying on globals from script tags.
- Run a linter such as ESLint with no-undef, which flags undeclared names as you type.