More JavaScript answers

by Danny
Published 2 min read

=== only checks whether two arrays or objects are the same reference. Learn how to compare their contents with every, a deep equality helper or Node's isDeepStrictEqual.

The short answer

#

=== and == compare objects and arrays by reference, so two separate arrays with the same items are not equal. To compare contents, check them yourself: length plus every for flat arrays, or a deep equality function for nested data. In Node, use util.isDeepStrictEqual.


_10
const a = [1, 2, 3];
_10
const b = [1, 2, 3];
_10
_10
a === b; // false, different arrays in memory
_10
_10
const sameItems = a.length === b.length && a.every((value, i) => value === b[i]);
_10
// true

Why it happens

#

Primitives such as numbers and strings are compared by value. Objects, and arrays are objects, are compared by identity: the question is "are these the same object?", not "do these look the same?". That is fast and well defined, but it means [] === [] is false.

JavaScript has no built-in deep equality operator for content. Deciding what "equal" means is also a choice: does key order matter, should NaN equal NaN, should a Date compare by time, and what about class instances? So you pick a method that matches your data.

How to fix it

#

Compare flat arrays with every

For arrays of primitives, check the length and then each position.


_10
function arraysEqual(a, b) {
_10
return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));
_10
}
_10
_10
arraysEqual([1, NaN], [1, NaN]); // true
_10
arraysEqual([1, 2], [2, 1]); // false

Object.is is used here so that NaN matches NaN. If order should not matter, sort copies first with toSorted(), or compare them as sets.

Compare shallow objects by their keys

For objects one level deep, such as settings or React props, compare the keys and values.


_10
function shallowEqual(a, b) {
_10
const keysA = Object.keys(a);
_10
const keysB = Object.keys(b);
_10
return (
_10
keysA.length === keysB.length &&
_10
keysA.every((key) => Object.hasOwn(b, key) && Object.is(a[key], b[key]))
_10
);
_10
}

Write or import a deep equality function

For nested data, recurse into arrays and plain objects.


_15
function deepEqual(a, b) {
_15
if (Object.is(a, b)) return true;
_15
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
_15
return false;
_15
}
_15
if (Array.isArray(a) !== Array.isArray(b)) return false;
_15
_15
const keysA = Object.keys(a);
_15
const keysB = Object.keys(b);
_15
if (keysA.length !== keysB.length) return false;
_15
_15
return keysA.every((key) => Object.hasOwn(b, key) && deepEqual(a[key], b[key]));
_15
}
_15
_15
deepEqual({ user: { tags: ['a'] } }, { user: { tags: ['a'] } }); // true

This covers plain JSON-like data. It does not handle Date, Map, Set or circular references. For those, use a tested library such as fast-deep-equal or Lodash's isEqual, rather than growing your own.

Use isDeepStrictEqual in Node

Node ships a deep comparison that understands dates, maps, sets and typed arrays.


_10
import { isDeepStrictEqual } from 'node:util';
_10
_10
isDeepStrictEqual({ a: [1, 2] }, { a: [1, 2] }); // true
_10
isDeepStrictEqual({ a: 1 }, { a: '1' }); // false

In tests, assert.deepStrictEqual from node:assert, or toEqual and toStrictEqual in Vitest and Jest, do the same job and print a helpful diff.

Use JSON.stringify only for quick checks

JSON.stringify(a) === JSON.stringify(b) works for simple data, but it is sensitive to key order and drops some values, so treat it as a shortcut for scripts, not application logic.

Common pitfalls

#
  • Relying on JSON.stringify when key order can differ. { a: 1, b: 2 } and { b: 2, a: 1 } produce different strings.
  • Forgetting that JSON.stringify turns undefined, functions and symbols into nothing and NaN into null, so different values can look equal.
  • Comparing with includes or indexOf on arrays of objects. They use reference equality too, so use some with your own check.
  • Running a deep comparison on every render or keystroke for large data. Compare an ID or version number instead when you can.
  • Mutating an object and expecting a change check to notice. If you keep the same reference, === says nothing changed, which is why React and many stores expect new objects.
#

Was this resource helpful?