How are objects compared with == or ===?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Objects #JS Basics

Brief Answer

Objects in JavaScript are compared by reference, not by content. Operators == and === return true only if both operands refer to the same object in memory. Even if objects look the same, they are not equal if they are different objects.


Full Answer

In JavaScript, objects are not compared like primitive values. It’s important to understand how comparison works to avoid errors.

How Object Comparison Works

Objects are compared by reference, not by value:

const obj1 = { name: 'John' };
const obj2 = { name: 'John' };
console.log(obj1 == obj2); // false
console.log(obj1 === obj2); // false

Why Objects Are Not Equal

Different Objects in Memory

const obj1 = { a: 1 };
const obj2 = { a: 1 };
// obj1 and obj2 are different objects in memory
console.log(obj1 === obj2); // false

Same Object

const obj1 = { a: 1 };
const obj2 = obj1; // Same reference
console.log(obj1 === obj2); // true

Simple Examples

Comparing Identical Objects

const user1 = { name: 'John', age: 25 };
const user2 = { name: 'John', age: 25 };
 
// Even with same data — not equal
console.log(user1 === user2); // false

Comparing References

const user = { name: 'John' };
const sameUser = user; // Same reference
 
console.log(user === sameUser); // true
console.log(user === { name: 'John' }); // false

Features of == and === with Objects

Both Operators Work the Same

const obj1 = { a: 1 };
const obj2 = { a: 1 };
 
// With objects == and === work the same
console.log(obj1 == obj2); // false
console.log(obj1 === obj2); // false

Comparison with null and undefined

const obj = { a: 1 };
 
console.log(obj == null); // false
console.log(obj === null); // false
console.log(obj == undefined); // false
console.log(obj === undefined); // false

When Objects Are Equal

Only Same Object

const user = { name: 'John' };
const admin = user; // Same reference
 
console.log(user === admin); // true
admin.name = 'Peter';
console.log(user.name); // 'Peter' — it's the same object

After Assignment

let obj1 = { a: 1 };
let obj2 = obj1;
 
console.log(obj1 === obj2); // true
obj2 = { a: 1 }; // New object
console.log(obj1 === obj2); // false

Common Mistakes

1. Expecting Content Equality

// ❌ Thinking objects with same data are equal
const obj1 = { name: 'John' };
const obj2 = { name: 'John' };
if (obj1 === obj2) {
  console.log('Equal'); // Won't trigger
}
 
// ✅ Proper understanding
console.log(obj1 === obj2); // false

2. Comparison with Primitives

// ❌ Comparing object with primitive
const obj = { toString() { return 'test'; } };
console.log(obj == 'test'); // true (conversion)
console.log(obj === 'test'); // false (different types)

Simple Rules

  1. By reference — objects are compared by reference, not content
  2. Same object — only same object is equal to itself
  3. == and === — work the same with objects
  4. Different objects — even with same data are not equal
  5. Reference — variables can reference same object

Understanding object comparison helps avoid errors when working with data and conditions in code.


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪