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

Why Two Empty Objects Are Not Equal

See that {} !== {} because each object literal creates a new reference in memory.

Why Two Empty Objects Are Not Equal

What you'll learn: Why {} !== {} in JavaScript, even though both objects look identical.

The Core Idea

When you create two empty objects in JavaScript, they might look the same, but they're stored in completely different locations in memory. Each object literal ({}) creates a new reference — a new "box" in the computer's memory.

When you compare objects with ===, JavaScript checks if both variables point to the exact same box, not whether the boxes contain the same items.

const obj1 = {};
const obj2 = {};

console.log(obj1 === obj2); // false

Even though both obj1 and obj2 are empty, they're different objects in memory.

The Memory Analogy

Think of objects like houses. Two empty houses might be identical in layout and contents (nothing!), but they're still two different buildings at different addresses. When JavaScript compares objects, it's checking: "Are these the same house?" not "Do these houses look the same?"

const house1 = {};
const house2 = {};
const house3 = house1;

console.log(house1 === house2); // false - different addresses
console.log(house1 === house3); // true - same address!

Why This Matters

This behavior catches many beginners off guard. You can't create two "identical" objects and expect them to be equal. Only when two variables reference the exact same object will === return true.

Key Takeaway: Every object literal {} creates a new reference in memory, so {} !== {} even though they appear identical. Object comparison checks reference identity, not contents.