Skip to content

How to Fix ReferenceError: X is not defined in JavaScript

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix ReferenceError: X is not defined in JavaScript. We cover key concepts, practical examples, and best practices.

The Problem

JavaScript throws ReferenceError: X is not defined when your code tries to access a variable that has not been declared or is not in the current scope.

Quick Fix

Step 1: Check for typos

Variable names are case-sensitive. Compare the name in the error to your declaration:

console.log(userName);
let userName = 'Alice';
ReferenceError: Cannot access 'userName' before initialization

Fix the spelling or move the declaration above the usage:

let userName = 'Alice';
console.log(userName);
Alice

Step 2: Declare with let, const, or var

In strict mode, assigning to an undeclared variable throws:

'use strict';
x = 10;
console.log(x);
ReferenceError: x is not defined

Add a declaration keyword:

'use strict';
let x = 10;
console.log(x);
10

Step 3: Verify scope boundaries

Block-scoped variables are not accessible outside their block:

if (true) {
    let msg = 'hello';
}
console.log(msg);
ReferenceError: msg is not defined

Declare the variable in the outer scope:

let msg;
if (true) {
    msg = 'hello';
}
console.log(msg);
hello

Step 4: Check script load order in HTML

If a script runs before the variable is defined in another script, you get a ReferenceError. Load dependencies first or use defer:

<script src="utils.js"></script>
<script src="main.js"></script>

Prevention

  • Always declare variables with let or const before first use.
  • Enable strict mode with 'use strict' to catch undeclared assignments.
  • Use ESLint with the no-undef rule enabled.
  • Use const by default and let only when reassignment is needed.

Common Mistakes with referenceerror

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world JS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between "not defined" and "undefined"?

"Not defined" means the variable has never been declared in any accessible scope. "Undefined" means the variable exists but has no assigned value. ReferenceError is thrown for the former; the latter simply returns undefined.

Why does typeof not throw for undeclared variables?

The typeof operator is the only construct in JavaScript that safely references undeclared variables. It returns the string "undefined" for undeclared names instead of throwing. This is a special case in the language specification designed for feature detection.

Does hoisting affect ReferenceError behavior?

Yes. Variables declared with var are hoisted and initialized to undefined, so accessing them before declaration returns undefined instead of throwing. Variables declared with let and const are hoisted but not initialized (temporal dead zone), so accessing them before declaration throws ReferenceError.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro