---
title: "How do I compare two arrays or objects for equality in JavaScript?"
description: "=== 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."
url: https://www.danny.engineering/answer/javascript/how-do-i-compare-two-arrays-or-objects-for-equality-in-javascript
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["Javascript"]
---

# How do I compare two arrays or objects for equality in JavaScript?

## 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`.


```javascript
const a = [1, 2, 3];
const b = [1, 2, 3];

a === b; // false, different arrays in memory

const sameItems = a.length === b.length && a.every((value, i) => value === b[i]);
// 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.


```javascript
function arraysEqual(a, b) {
  return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));
}

arraysEqual([1, NaN], [1, NaN]); // true
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.


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


### Write or import a deep equality function


For nested data, recurse into arrays and plain objects.


```javascript
function deepEqual(a, b) {
  if (Object.is(a, b)) return true;
  if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
    return false;
  }
  if (Array.isArray(a) !== Array.isArray(b)) return false;

  const keysA = Object.keys(a);
  const keysB = Object.keys(b);
  if (keysA.length !== keysB.length) return false;

  return keysA.every((key) => Object.hasOwn(b, key) && deepEqual(a[key], b[key]));
}

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.


```javascript
import { isDeepStrictEqual } from 'node:util';

isDeepStrictEqual({ a: [1, 2] }, { a: [1, 2] }); // true
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.

## Related reading

- [MDN: Equality comparisons and sameness](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness)
- [Node.js: util.isDeepStrictEqual()](https://nodejs.org/api/util.html)
