=== 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.
_10const a = [1, 2, 3];_10const b = [1, 2, 3];_10_10a === b; // false, different arrays in memory_10_10const 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.
_10function arraysEqual(a, b) {_10 return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));_10}_10_10arraysEqual([1, NaN], [1, NaN]); // true_10arraysEqual([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.
_10function 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.
_15function 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_15deepEqual({ 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.
_10import { isDeepStrictEqual } from 'node:util';_10_10isDeepStrictEqual({ a: [1, 2] }, { a: [1, 2] }); // true_10isDeepStrictEqual({ 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.stringifywhen key order can differ.{ a: 1, b: 2 }and{ b: 2, a: 1 }produce different strings. - Forgetting that
JSON.stringifyturnsundefined, functions and symbols into nothing andNaNintonull, so different values can look equal. - Comparing with
includesorindexOfon arrays of objects. They use reference equality too, so usesomewith 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.
Related reading
#Was this resource helpful?