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

Reference Equality vs Value Equality

Understand why two objects with identical properties are not equal when compared with === or ==.

Reference Equality vs Value Equality

What you'll learn: Why two objects with identical properties aren't considered equal when compared with === or ==.

The Core Idea

When you compare two objects in JavaScript using === (or ==), you're checking if they point to the same location in memory—not whether their contents match. This is called reference equality.

Think of it like house addresses: two houses can look identical inside, but if they're at different addresses, they're different houses. JavaScript compares the addresses (references), not what's inside.

Seeing It in Action

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

console.log(person1 === person2); // false — different references!

const person3 = person1;
console.log(person1 === person3); // true — same reference!

Even though person1 and person2 have identical properties, they're stored at different memory locations. When you created each object, JavaScript allocated separate spaces for them.

However, person3 doesn't create a new object—it just points to the same memory location as person1. They share the same reference, so the comparison returns true.

Why This Matters

This explains why you can't simply use === to check if two objects "look the same." The operator only tells you if they're the exact same object in memory, not if their contents match.

Remember from the previous lesson: objects store references, not values. When you compare objects, you're comparing those references.

Key Takeaway: Two objects with identical properties are not equal with === or == because JavaScript compares their memory references, not their contents. Only objects pointing to the same memory location are considered equal.