V8-based (Chrome, Edge) and Firefox
- Message
- ReferenceError: "x" is not defined
A ReferenceError means your code used a name that doesn't exist in that scope. Fix typos, scope, load order for jQuery and libraries, and let and const timing.
ReferenceError: x is not defined means JavaScript ran code that uses a variable or function name that doesn't exist in the current scope. Check the spelling, declare the variable, move it into scope, and load any library script before the code that uses it. The cause is in the script, so the fix is in the code or the order it loads.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
Treat the name in the message as the whole clue. Work out why that exact name wasn't available at that moment: wrong name, wrong place, or wrong time.
function numbers() {
const num1 = 2;
const num2 = 3;
return num1 + num2;
}
console.log(num1); // ReferenceError: num1 is not definedMDN says a variable needs to be available in the current context of execution. A function can use variables from the scope it was defined in, so a function defined in the global scope can read global variables, but nothing outside a function can read that function's locals.
defer, MDN says deferred scripts execute in the order they appear in the document, so list the library first.Two cautions from MDN. Scripts with async give no guarantee of running order, so don't use it for a library and the code that depends on it. And a deferred script runs after the document is parsed, so inline code that runs immediately can't use a name a deferred script defines. If the library file fails to load, the name never appears at all; check the Network panel for a failed request first.
<script defer src="js/vendor/jquery.js"></script>
<script defer src="js/app.js"></script>let or const variables are accessed before the place where they are declared has been executed, and that "it is the execution order of access and variable declaration that matters, not the order in which the statements appear in the code." It doesn't happen with var, which is initialized to undefined when hoisted. It can also show up in cyclic imports.if (typeof jQuery !== "undefined") {
jQuery(".menu").addClass("ready");
}If the failing code comes from a tag manager or another third party, the same rules apply, but the fix is the order or conditions of that tag. A sibling error you may hit next is Cannot read properties of undefined.
Open DevTools, then Console. The error links to the file and line. Click it.
Is it spelled the same everywhere? Is it declared anywhere in a scope that reaches this line?
Open the Network panel and Sources. Confirm the library file loaded without an error and appears before your script.
If it is a let or const, find where the declaration runs relative to the line that fails.
This error is raised by the browser's JavaScript engine, not returned by your server. If a script that builds content or links throws before it finishes, the page may render without them. See JavaScript SEO and client-side rendering.
4 questions people ask next, answered from the sources on this page.
The code used a variable or function name that doesn't exist in the current scope at the moment the line ran. Check the spelling, the scope and the load order.
Load jQuery before any code that uses it. MDN says to put the script element that loads the library before your code, and with defer the scripts run in document order.
MDN says it represents an error when a variable that doesn't exist, or hasn't yet been initialized, is referenced in the current scope.
No. A variable that is declared but has no value is undefined, and reading it is fine. A name that was never declared in scope throws a ReferenceError. typeof on an undeclared name returns "undefined" without throwing.
Run the free audit, then approve the fixes. Every change can be undone.