Problem Statement
What is the result of {a: 1} === {a: 1}?
Explanation
Objects are compared by reference, not by value.
Even if two objects have identical properties, they are different objects in memory.
Triple equals checks if both variables point to the same object in memory.
To compare object contents, you need to manually compare each property or use a deep equality function.
This is a common source of confusion for beginners.
Code Solution
SolutionRead Only
// Objects compared by reference
console.log({a: 1} === {a: 1}); // false (different objects)
// Same reference
const obj1 = {a: 1};
const obj2 = obj1; // Same reference
console.log(obj1 === obj2); // true
// Primitives compared by value
console.log(5 === 5); // true
console.log('hi' === 'hi'); // true
// Array comparison (also by reference)
console.log([1, 2] === [1, 2]); // false
const arr1 = [1, 2];
const arr2 = arr1;
console.log(arr1 === arr2); // true
// Compare object contents manually
function areEqual(obj1, obj2) {
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;
for (let key of keys1) {
if (obj1[key] !== obj2[key]) return false;
}
return true;
}
console.log(areEqual({a: 1}, {a: 1})); // true