Back to JavaScript
2026-05-076 min read

ReferenceError: "x" is not defined (JavaScript)

Learn ReferenceError: "x" is not defined (JavaScript) step by step with clear examples and exercises.

Title: Mastering "ReferenceError: 'x' is not defined" in JavaScript

Why This Matters

In JavaScript, a ReferenceError occurs when you try to use a variable that hasn't been declared or assigned a value yet. This common mistake can lead to runtime errors and cause your code to break unexpectedly. Understanding how to identify and fix this error is crucial for writing robust and reliable JavaScript code.

Prerequisites

Before diving into the core concept, you should be familiar with:

  • Basic JavaScript syntax, including variables, data types, and operators
  • How to declare variables using var, let, or const
  • Functions and function declarations
  • Understanding of blocks, scopes, and hoisting

Core Concept

The ReferenceError: 'x' is not defined error is thrown when you try to use a variable named x that hasn't been declared or assigned a value. To avoid this error, make sure to declare your variables before using them in your code and ensure there are no typos in their names.

let x; // Declaring an undefined variable 'x'
console.log(x); // Throws ReferenceError: 'x' is not defined

// Correct way to declare and assign a value to 'x'
let x = 10;
console.log(x); // Outputs 10

In the example above, we first declare an undefined variable x. When we try to log its value to the console, JavaScript throws a ReferenceError. To fix this error, we need to assign a value to x before using it.

Hoisting and Variable Scope

JavaScript has a feature called hoisting, which moves declarations to the top of their containing scope. However, assignments are not hoisted, so you'll still get a ReferenceError if you try to use an undeclared variable before it is assigned a value:

console.log(x); // Throws ReferenceError: 'x' is not defined
let x = 10;

In this example, the declaration of x is hoisted to the top of its scope, but the assignment isn't. As a result, when we try to log x before it has been assigned a value, JavaScript throws a ReferenceError.

Function Scope and Lifetime

Variables in JavaScript have function scope, meaning they are only accessible within the function or block where they were declared. When a function is called multiple times, each call creates a new scope with its own variables:

function example() {
let x = 10; // Declare and assign 'x' in each function call
console.log(x);
}

example(); // Outputs 10
example(); // Outputs undefined (since a new scope is created for each function call)

In this example, we declare and assign x within the example function. When we call example() twice, JavaScript creates two separate scopes, each with its own copy of x. The first call initializes x to 10, but when we call it again, x is still undefined because a new scope is created with an empty x.

Global Scope and Window Object

If you don't declare a variable within a function or block, it will be added to the global scope (or the window object in a browser environment). This can lead to unintended conflicts between variables:

let x = 10; // Declare and assign 'x' in the global scope

function example() {
console.log(x); // Outputs 10 (since 'x' is in the global scope)
}

example();

In this example, we declare and assign x in the global scope. When we call the example function, it has access to the global x. If you want to prevent this behavior, always declare and assign variables within their appropriate scopes.

Worked Example

Let's walk through a worked example that demonstrates how to identify and fix a ReferenceError: 'x' is not defined error in JavaScript.

function calculateSum(a, b) {
let result = a + b; // Declare and assign 'result' but use 'x' instead by mistake
console.log(`The sum of ${a} and ${b} is ${x}`); // Throws ReferenceError: 'x' is not defined
}

calculateSum(5, 3); // Error thrown here

In the example above, we have a function calculateSum that calculates the sum of two numbers and logs the result. However, we accidentally use x instead of result when logging the output. To fix this error, simply replace x with result:

function calculateSum(a, b) {
let result = a + b; // Declare and assign 'result' but use 'x' instead by mistake
console.log(`The sum of ${a} and ${b} is ${result}`); // Replace 'x' with 'result' to fix the error
}

calculateSum(5, 3); // No longer throws an error

Common Mistakes

  1. Declaring but not assigning a value to a variable (e.g., let x; console.log(x)).
  2. Using undeclared variables before they are declared (e.g., console.log(x); let x = 10).
  3. Forgetting to declare function's parameters, causing them to be treated as global variables (e.g., function example() { console.log(x) }).
  4. Using the same variable name in multiple scopes, leading to unintended conflicts (e.g., declaring a local and global x).
  5. Assuming that function declarations are hoisted, but not their assignments (e.g., function example() { console.log(x) } example(); let x = 10).
  6. Using var instead of let or const, which can lead to unexpected global variable creation and conflicts (e.g., var x; function foo() { console.log(x); let x = 'bar'; } foo();).

Subheadings under Common Mistakes:

  • Using var instead of let or const for local variables
  • Declaring multiple variables with the same name in the same scope
  • Assuming that all function declarations are hoisted, including assignments

Practice Questions

  1. Write a JavaScript function that calculates the sum of two numbers and logs the result using variables named a, b, and result. Make sure to declare all variables before using them.
  2. Given the following code, what is the output when you call example()? Why does it throw an error? How can you fix the error?
function example() {
let x = 10; // Declare and assign 'x' but use 'y' instead by mistake
console.log(`The value of y is ${y}`); // Throws ReferenceError: 'y' is not defined
}

example();
  1. Write a JavaScript function that takes an array of numbers as its argument and returns the sum of all the elements in the array. Make sure to handle cases where the array is empty or contains non-numeric values.
  2. What is the difference between var, let, and const when it comes to variable hoisting? Provide examples for each.
  3. Given the following code, what is the output when you call example()? Why does it throw an error? How can you fix the error?
function example() {
let x = 10; // Declare and assign 'x' but use 'y' instead by mistake
console.log(`The value of y is ${y}`); // Throws ReferenceError: 'y' is not defined
}

example();

FAQ

Why do I get a ReferenceError: 'x' is not defined error even though I declared the variable using var, let, or const?

  • Ensure that you are declaring your variables before using them, and that there are no typos in their names.

What happens if I declare a variable with the same name multiple times within the same scope?

  • In JavaScript, variables are only allowed to have one value at a time. If you declare a variable with the same name multiple times within the same scope, the last declaration will overwrite the previous ones.

Can I use let or const instead of var to avoid ReferenceError: 'x' is not defined errors?

  • Yes! Using let and const instead of var helps you avoid some common pitfalls, such as creating global variables by accident or declaring multiple variables with the same name within the same scope.

What are some best practices for avoiding ReferenceError: 'x' is not defined errors in my JavaScript code?

  • Always declare your variables before using them, preferably at the top of their containing function or block. Use meaningful variable names that reflect their purpose. Avoid using the same variable name in multiple scopes and ensure that you are assigning values to all declared variables. Use let and const instead of var for local variables.
ReferenceError: "x" is not defined (JavaScript) | JavaScript | XQA Learn