Sub topic 11
Comparing Objects and Arrays by Reference
This chapter assumes you know how strict equality (===) works with primitives. It also assumes you know that == converts its operands to primitives with ToPrimitive. Here you will see what both operators do when the operands are objects, arrays, or functions.
Identity, Not Content
A variable that holds an object stores a reference to it, which tells the engine where the object lives in memory. When both operands are objects, === and == compare those references. The result is true only if both sides point to the same object. Matching properties and values do not count.
const a = { x: 1 };
const b = { x: 1 };
const c = a;
console.log(a === b); // false: different objects
console.log(a === c); // true: same reference
console.log([1, 2] === [1, 2]); // false
console.log([1, 2] == '1,2'); // true: the array is converted to the string '1,2'Since c and a share one object, a change made through c is visible through a. Declaring them with const doesn't prevent this. const stops the variable from being reassigned, but the object itself can still be changed.
When == Converts an Object
== only behaves differently from === when one side is an object and the other is a primitive. In that case the object goes through ToPrimitive. That is why [1, 2] == '1,2' is true. If both sides are objects, no conversion happens, so [] == [] is false.
Common Errors
Several built-in tools compare items by identity, and this can surprise you:
const list = [{ id: 1 }];
console.log(list.includes({ id: 1 })); // false: a new object literal
const item = list[0];
console.log(list.includes(item)); // true: same reference
const set = new Set([{ id: 1 }, { id: 1 }]);
console.log(set.size); // 2: two distinct objectsindexOf, includes, Set, and Map keys all use identity when they compare objects. If you want to remove duplicates by content, compare a primitive key such as id.
Comparing by Content
To compare by content, write the comparison yourself. A shallow comparison checks only the top level:
function shallowEqualArrays(x, y) {
if (x === y) return true;
if (x.length !== y.length) return false;
return x.every((value, i) => Object.is(value, y[i]));
}
console.log(shallowEqualArrays([1, 2], [1, 2])); // true
console.log(shallowEqualArrays([NaN], [NaN])); // true
console.log(shallowEqualArrays([{ a: 1 }], [{ a: 1 }])); // false: nested objects differ by referenceFor nested structures, use a recursive deep-equality function, or a tested library function such as Node's util.isDeepStrictEqual.
Exercise
What does this code log? const p = [1]; const q = p; q.push(2); console.log(p === q, p, [0] == [0], [0] == 0);
Show answer
It logs true [1, 2] false true. p and q reference the same array, so the push is visible through p. [0] == [0] compares two separate arrays by identity. [0] == 0 converts the array to the string '0' and then to the number 0.
Exercise
Why does new Set([[1], [1]]).size return 2, and how could you count unique arrays by content?
Show answer
A Set compares objects by reference, and each [1] literal creates a new array. One way to count by content is to map each array to a primitive key first, for example new Set(arrays.map(arr => arr.join(','))).size. This works when the elements are simple values whose string form is unambiguous.