Object and Array Comparison
You'll learn why comparing objects and arrays in JavaScript often surprises beginners—and how to work with their identity-based behavior.
Why Reference Types Compare Differently
In JavaScript, objects and arrays are reference types. This means when you create an object or array, JavaScript stores it in memory and gives you a reference (like an address) to that location—not the actual data itself.
When you use comparison operators (===, ==, !==, !=) with objects or arrays, JavaScript compares the references, not the contents.
What This Looks Like
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
console.log(arr1 === arr2); // false (different references)
const obj1 = { name: "Alice" };
const obj2 = { name: "Alice" };
console.log(obj1 === obj2); // false (different objects in memory)
Even though the arrays contain the same values and the objects have identical properties, they're not equal because they exist at different memory locations.
When They Are Equal
const arr1 = [1, 2, 3];
const arr2 = arr1; // arr2 points to the SAME array
console.log(arr1 === arr2); // true (same reference)
Here, arr2 isn't a copy—it's another name pointing to the exact same array in memory.
The House Analogy
Think of it like houses: two houses at different addresses can look identical inside, but they're still different houses. The comparison checks the address (reference), not whether the furniture matches (contents).
Key Takeaway: Objects and arrays compare by identity (reference), not by their contents. Two separate objects with identical properties are never equal using ===.