Free auditStart free
GuideError · Browser

Uncaught 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.

4 min
read
5
sources
Oct 22, 2026
updated
Scaplet Editorial Team
author
In one line

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.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01The message in each browser, per MDN

What the error means and who causes it

MDN describes this exception as occurring "when there is a non-existent variable referenced somewhere." The ReferenceError object, per MDN, represents "an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced." The browser isn't failing and no server is involved. Your script, or a third-party script on the page, used a name that wasn't there when that line ran.
Browser engine

V8-based (Chrome, Edge) and Firefox

Message
ReferenceError: "x" is not defined
Browser engine

Safari

Message
ReferenceError: Can't find variable: x

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.

03Check it

jQuery, $ and other library names

MDN's note on this error is short and specific: when loading a library such as jQuery, make sure it is loaded before you access library variables such as "$". That means the script element that loads the library must come before your code that uses it. If you load scripts with 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.

Library first, your code second
<script defer src="js/vendor/jquery.js"></script>
<script defer src="js/app.js"></script>
04Good to know

let, const and the "before initialization" variant

A related ReferenceError reads "Cannot access 'X' before initialization" in V8-based browsers, "can't access lexical declaration 'X' before initialization" in Firefox and "Cannot access uninitialized variable." in Safari. MDN says it happens when 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.
A safe check for an optional global
if (typeof jQuery !== "undefined") {
  jQuery(".menu").addClass("ready");
}
05How it works

Track it down in two minutes

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.

  1. 01

    Read the file and line

    Open DevTools, then Console. The error links to the file and line. Click it.

  2. 02

    Search for the name

    Is it spelled the same everywhere? Is it declared anywhere in a scope that reaches this line?

  3. 03

    Check the load order

    Open the Network panel and Sources. Confirm the library file loaded without an error and appears before your script.

  4. 04

    Check the timing

    If it is a let or const, find where the declaration runs relative to the line that fails.

06Explained

What it does to SEO

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.

Questions

Asked a lot.

4 questions people ask next, answered from the sources on this page.

01What does "not defined" mean in JavaScript?

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.

02How do I fix "jQuery is not defined"?

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.

03What is a ReferenceError?

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.

04Is not defined the same as undefined?

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.

Sources · 5

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • mozilla.org
  1. 01MDN: ReferenceError: "x" is not defineddeveloper.mozilla.org · checked 2026-10-25
  2. 02MDN: ReferenceErrordeveloper.mozilla.org · checked 2026-10-25
  3. 03MDN: ReferenceError: can't access lexical declaration 'X' before initializationdeveloper.mozilla.org · checked 2026-10-25
  4. 04MDN: typeofdeveloper.mozilla.org · checked 2026-10-25
  5. 05MDN: script element (async and defer)developer.mozilla.org · checked 2026-10-25
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane