Course contentsShow
← JavaScript
Lesson 60 of 2,7232. Values and TypesFree lesson

Comparing Objects by Reference

Discover that objects are compared by reference identity, not by their properties or structure.

Comparing Objects by Reference

What you'll learn: Why two objects with identical properties are not considered equal in JavaScript.

The Core Idea

When you compare objects using === (or ==), JavaScript checks whether both variables point to the exact same object in memory—not whether they have the same properties or values. Two objects can look identical but still fail an equality test if they're separate instances.

Think of it like house addresses: two houses might have identical floor plans, paint colors, and furniture, but they're different houses at different addresses. JavaScript cares about the address (reference), not the contents.

How It Works

const person1 = { name: "Alice" };
const person2 = { name: "Alice" };

console.log(person1 === person2); // false

Even though both objects have the same property and value, they're stored in different memory locations. They're separate objects, so the comparison returns false.

However, if two variables reference the same object:

const person1 = { name: "Alice" };
const person2 = person1; // person2 now points to the same object

console.log(person1 === person2); // true

Now both variables hold the same reference—they point to the identical object in memory.

Why This Matters

This behavior catches many beginners off guard. You might expect objects with matching properties to be equal, but JavaScript doesn't inspect their contents during comparison—it only checks if they're the same object.

Key Takeaway: JavaScript compares objects by their reference identity, not by their properties or structure. Two separate objects are never equal, even if they look identical.