Referential Identity in JavaScript
You'll learn why two objects that look identical are not considered equal in JavaScript.
The Core Idea
In JavaScript, every time you create an object or function, you're creating a unique instance with its own identity in memory. Even if two objects contain the exact same data, they are not the same object—they're different entities that happen to look alike.
Think of it like houses: two houses might have identical floor plans, furniture, and paint colors, but they're still different houses at different addresses. JavaScript uses the "address" (reference) to compare objects, not their contents.
How This Affects Equality
const user1 = { name: "Alice", age: 30 };
const user2 = { name: "Alice", age: 30 };
console.log(user1 === user2); // false — different objects!
const user3 = user1;
console.log(user1 === user3); // true — same reference
The same applies to arrays and functions:
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
console.log(arr1 === arr2); // false
const fn1 = () => console.log("hello");
const fn2 = () => console.log("hello");
console.log(fn1 === fn2); // false
Why This Matters in React
React relies heavily on === comparison to determine if values have changed. If you create a new object or function on every render (even with identical contents), React sees it as a different value. This triggers unnecessary re-renders and breaks optimizations like React.memo and dependency arrays.
Key Takeaway: In JavaScript, objects and functions are compared by reference (identity), not by their contents. Creating a new object/function creates a new identity, even if the data inside is identical.