# Uncaught ReferenceError: x is not defined

> A ReferenceError means your code used a name that doesn

Canonical: https://scaplet.com/errors/browser/uncaught-referenceerror-is-not-defined

Guide Error · Browser

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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-referenceerror-is-not-defined%20(%22Uncaught%20ReferenceError%3A%20x%20is%20not%20defined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-referenceerror-is-not-defined%20(%22Uncaught%20ReferenceError%3A%20x%20is%20not%20defined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-referenceerror-is-not-defined%20(%22Uncaught%20ReferenceError%3A%20x%20is%20not%20defined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fbrowser%2Funcaught-referenceerror-is-not-defined%20(%22Uncaught%20ReferenceError%3A%20x%20is%20not%20defined%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

On this page

## 6 parts, 4 minutes.

1. [What the error means and who causes it 01](#what-the-error-means-and-who-causes-it)
2. [The four usual causes 02](#the-four-usual-causes)
3. [jQuery, $ and other library names 03](#jquery-and-other-library-names)
4. [let, const and the "before initialization" variant 04](#let-const-and-the-before-initialization-variant)
5. [Track it down in two minutes 05](#track-it-down-in-two-minutes)
6. [What it does to SEO 06](#what-it-does-to-seo)

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

_02_Cause, example and fix

## The four usual causes

Cause

### Never declared or misspelled

What it looks like

foo.substring(1) when foo was never declared

Fix

Declare it (const foo = "bar";) or correct the spelling

Cause

### Wrong scope

What it looks like

A variable defined inside a function is used outside it

Fix

Move the declaration to a scope both places share, or return the value

Cause

### Library not loaded yet

What it looks like

$ or jQuery is used before its script has run

Fix

Put the script element that loads the library before your code that uses it

Cause

### let or const used too early

What it looks like

Cannot access 'X' before initialization

Fix

Run the declaration before the access

Wrong scope, as MDN shows it

```
function numbers() { const num1 = 2; const num2 = 3; return num1 + num2; } console.log(num1); // ReferenceError: num1 is not defined
```

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

_03_Check 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>
```

_04_Good 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"); }
```

typeof doesn't throw for undeclared names

MDN says `typeof` works with undeclared identifiers and returns "undefined" instead of throwing. That makes `typeof maybeLibrary !== "undefined"` a safe test for an optional global. It still throws for `let` or `const` in the same block before the declaration.

_05_How it works

## Track it down in two minutes

If the failing code comes from a [tag manager](/glossary/google-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](/errors/browser/uncaught-typeerror-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.

_06_Explained

## 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](/glossary/javascript-seo) and [client-side rendering](/glossary/client-side-rendering).

Related errors: [Module not found: Can't resolve](/errors/misc/module-not-found-can-t-resolve), [Text content does not match server-rendered HTML](/errors/browser/text-content-does-not-match-server-rendered-html).

Questions

## Asked a lot.

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

_01_ What 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.

_02_ How 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.

_03_ What 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.

_04_ Is 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. 01 [MDN: ReferenceError: "x" is not defined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined) developer.mozilla.org · checked 2026-10-25
2. 02 [MDN: ReferenceError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) developer.mozilla.org · checked 2026-10-25
3. 03 [MDN: ReferenceError: can't access lexical declaration 'X' before initialization](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init) developer.mozilla.org · checked 2026-10-25
4. 04 [MDN: typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) developer.mozilla.org · checked 2026-10-25
5. 05 [MDN: script element (async and defer)](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script) developer.mozilla.org · checked 2026-10-25

## Keep learning

[All errors](/errors)

[Learn Uncaught TypeError: Cannot read properties of undefined Browser errors](/errors/browser/uncaught-typeerror-cannot-read-properties-of-undefined) [Related Client-side rendering Crawling and indexing](/glossary/client-side-rendering) [Read next Text content does not match server-rendered HTML Browser errors](/errors/browser/text-content-does-not-match-server-rendered-html) [Do Run a free site audit Free, about two minutes](/site-audit)

More Uncaught errors
[Uncaught TypeError: Cannot read properties of undefined](/errors/browser/uncaught-typeerror-cannot-read-properties-of-undefined)

More in Browser errors
[Access-Control-Allow-Origin](/errors/browser/access-control-allow-origin) [Aw, Snap!](/errors/browser/aw-snap) [blocked:mixed-content](/errors/browser/blocked-mixed-content) [Chrome out of memory](/errors/browser/chrome-out-of-memory) [Firefox Server Not Found](/errors/browser/firefox-server-not-found) [Firefox: "Secure Connection Failed"](/errors/browser/firefox-secure-connection-failed) [Firefox: File not found](/errors/browser/firefox-file-not-found) [Firefox: the connection has timed out](/errors/browser/firefox-the-connection-has-timed-out) [Firefox: Warning: Potential Security Risk Ahead](/errors/browser/firefox-warning-potential-security-risk-ahead) [Refused to apply style ... because its MIME type](/errors/browser/refused-to-apply-style-from-because-its-mime-type) [Safari can't find the server](/errors/browser/safari-can-t-find-the-server) [Safari can't open the page](/errors/browser/safari-can-t-open-the-page) [Safari: "This Connection Is Not Secure" and related warnings](/errors/browser/safari-this-connection-is-not-private) [SSL certificate problem: unable to get local issuer certificate](/errors/browser/ssl-certificate-problem-unable-to-get-local-issuer-certificate) [Text content does not match server-rendered HTML](/errors/browser/text-content-does-not-match-server-rendered-html) [This page isn't working](/errors/browser/this-page-isn-t-working) [This site can't provide a secure connection](/errors/browser/this-site-can-t-provide-a-secure-connection) [Uncaught ReferenceError: x is not defined](/errors/browser/uncaught-referenceerror-is-not-defined) [Uncaught TypeError: Cannot read properties of undefined](/errors/browser/uncaught-typeerror-cannot-read-properties-of-undefined) [Your connection is not private](/errors/browser/your-connection-is-not-private) [Your connection is not private in Chrome (on every site)](/errors/browser/your-connection-is-not-private-chrome)

Terms on this page
[Google Tag Manager](/glossary/google-tag-manager) [JavaScript SEO](/glossary/javascript-seo) [Client-side rendering](/glossary/client-side-rendering)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
