# JavaScript: “ReferenceError: x is not defined”

> ReferenceError: x is not defined (Safari: Can't find variable). A typo, a variable out of scope, a script loaded too late, or a global that this runtime lacks.

Canonical: https://app369.com/fix/javascript-is-not-defined/
Last updated: 2026-11-08

Code stops with ReferenceError: myVar is not defined (Chrome, Node.js, Firefox) or Can't find variable: myVar (Safari). JavaScript looked up a name in every scope it could see and found nothing. Unlike undefined, which means a variable exists but has no value, this means the name itself does not exist where the code runs.

**Short answer**

Check the exact spelling and letter case of the name first. Then check where it was declared: let and const only exist inside the block where they are declared, so a variable created inside an if or a function is not visible outside it. For library globals such as $ or gtag, make sure the script that defines them loads before your code. For require, process, window or document, the code is running in an environment that does not have them; see the related fixes below.

## **Why the name does not exist here, and the fix**

1. 01 ### **A typo or different letter case**
  > “There is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in your current script or scope.” [**MDN, ReferenceError: "x" is not defined**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined) userId and userID are different names. Search the file for the exact spelling, and let your editor’s rename tool change every use at once.
2. 02 ### **Declared in a different scope** A variable declared with let or const inside a block, loop or function does not exist outside it. Declare it in the scope where both the assignment and the use can see it.```
// wrong
if (loggedIn) {
  const name = user.name;
}
console.log(name); // ReferenceError

// right
let name = 'Guest';
if (loggedIn) {
  name = user.name;
}
```
3. 03 ### **A library script that has not loaded yet**
  > “When loading a library (such as jQuery), make sure it is loaded before you access library variables, such as "$".” [**MDN, ReferenceError: "x" is not defined**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined) Globals such as $, gtag or Stripe exist only after their script runs. A script tag placed after yours, or loaded with async, may not have run yet. Load it first, or wait for its load event.```
<script src="https://js.stripe.com/v3/"></script>
<script src="/js/checkout.js" defer></script>
```
4. 04 ### **An undeclared assignment in a module or strict mode** Old scripts could write total = 0 without declaring it. ES modules and strict mode forbid that, so the same line now throws. Add let or const.```
let total = 0;
```

Checked against the vendor’s docs on November 8, 2026: [MDN: ReferenceError: "x" is not defined](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_defined).

## **Rather hand it over?**

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

- [from $450**App Fix Ticket**One reproducible bug in an existing mobile or web app, diagnosed and fixed.**See scope and start**](https://app369.com/fixed-price/flutter-firebase-bug-fix/)
- [from $450**AI-Built App Rescue**A Lovable, Bolt or Cursor app checked end to end in 3 days, then its worst problems fixed at a fixed price.****No issues worth fixing, no charge.**** If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.**See scope and start**](https://app369.com/fixed-price/ai-app-rescue/)

Related: [**“require is not defined”**](https://app369.com/fix/require-is-not-defined/) · [**“process is not defined”**](https://app369.com/fix/process-is-not-defined/) · [**Next.js “window is not defined”**](https://app369.com/fix/nextjs-window-is-not-defined/)

Questions

## **More on this error**

<details>

<summary>**What is the difference between not defined and undefined? **+</summary>

undefined is a value: the variable exists but holds nothing. not defined is an error: no variable with that name exists in any scope the code can see.

</details>

<details>

<summary>**Is typeof x a safe check? **+</summary>

Yes. typeof on an undeclared name returns 'undefined' instead of throwing, so typeof gtag === 'function' is a safe way to check whether a script has loaded. The exception is a let or const name used before its declaration runs, which still throws.

</details>

<details>

<summary>**Can you fix it for me? **+</summary>

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

</details>

A different error?

## **Describe it and get a written quote**

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.

[**Describe your problem **→](https://app369.com/contact/) [**All fixes**](https://app369.com/fix/)