How to Check Array Equality in TypeScript (5 Ways)

To check array equality in TypeScript, compare the lengths and then every element by index. === on two arrays only tells you whether they’re the same object, so build a small typescript array equality helper:

const equal = a.length === b.length && a.every((v, i) => v === b[i]);

Strict mode was on for all of these snippets, with TypeScript 7.0.2 and Node.js v22.22.2 doing the work, so what you see below is real terminal output.

Why does === return false for two identical arrays?

Arrays are objects, and === compares object references, not contents. Two literals with the same values are still two separate arrays:

const a = [1, 2, 3];
const b = [1, 2, 3];
const c = a;

console.log("a === b:", a === b);   // two different arrays
console.log("a === c:", a === c);   // same array, two names

Output:

a === b: false
a === c: true

Only c, which points at the same array as a, comes back true. == behaves the same way here; my post on loose vs strict equality in TypeScript explains why.

Compare arrays with JSON.stringify (and where it fails)

Comparing both arrays as JSON text is the quickest trick, since you’re really doing a string comparison in TypeScript. It works for flat arrays, but watch what it gets wrong:

const same = (x: unknown[], y: unknown[]) => JSON.stringify(x) === JSON.stringify(y);

console.log("[1,2,3] vs [1,2,3]:      ", same([1, 2, 3], [1, 2, 3]));
console.log("[1,2,3] vs [3,2,1]:      ", same([1, 2, 3], [3, 2, 1]));
console.log("[1,null] vs [1,undefined]:", same([1, null], [1, undefined]));
console.log("[{a:1,b:2}] vs [{b:2,a:1}]:", same([{ a: 1, b: 2 }], [{ b: 2, a: 1 }]));

Output:

[1,2,3] vs [1,2,3]:       true
[1,2,3] vs [3,2,1]:       false
[1,null] vs [1,undefined]: true
[{a:1,b:2}] vs [{b:2,a:1}]: false
Command Prompt output of TypeScript JSON.stringify array comparison: null equals undefined, NaN equals null, and reordered object keys are not equal
null and undefined both serialize to null, and objects with the same keys in another order look different.

It also throws on circular references and on BigInt, so I keep it for quick tests only.

Write a typed arraysEqual function with every()

This is the version I paste into projects. It returns early when the array lengths differ, then checks each position with Object.is, which treats NaN as equal to itself:

function arraysEqual<T>(a: readonly T[], b: readonly T[]): boolean {
  if (a === b) return true;
  if (a.length !== b.length) return false;
  return a.every((value, i) => Object.is(value, b[i]));
}

const zipCodes = ["10001", "60601", "94105"];
console.log(arraysEqual(zipCodes, ["10001", "60601", "94105"]));
console.log(arraysEqual([1, null], [1, undefined]));
console.log(arraysEqual([NaN], [NaN]));

Output:

true
false
true
Command Prompt output of a generic TypeScript arraysEqual function using length and every with Object.is
Matching ZIP codes pass, null vs undefined fails, and [NaN] equals [NaN].

The generic T is a bonus: comparing a number[] with a string[] won’t compile.

How do I compare two arrays when order doesn’t matter?

For two lists of state codes in any order, compare sorted copies. toSorted() (ES2023) leaves the originals alone, unlike sort(); see sorting arrays in TypeScript for custom comparers. Counting with a Map works for any value type:

function sameItems<T>(a: readonly T[], b: readonly T[]): boolean {
  if (a.length !== b.length) return false;
  const counts = new Map<T, number>();
  for (const v of a) counts.set(v, (counts.get(v) ?? 0) + 1);
  for (const v of b) {
    const n = counts.get(v);
    if (!n) return false;
    counts.set(v, n - 1);
  }
  return true;
}

const monday = ["NY", "TX", "CA"];
const tuesday = ["CA", "NY", "TX"];
console.log("sorted copies:", monday.toSorted().join() === tuesday.toSorted().join());
console.log("sameItems:    ", sameItems(monday, tuesday));
console.log("monday still: ", monday);

const setEqual = (a: string[], b: string[]) =>
  a.length === b.length && a.every((v) => new Set(b).has(v));
console.log("Set trap:     ", setEqual(["NY", "NY", "TX"], ["NY", "TX", "TX"]));
console.log("sameItems:    ", sameItems(["NY", "NY", "TX"], ["NY", "TX", "TX"]));

Output:

sorted copies: true
sameItems:     true
monday still:  [ 'NY', 'TX', 'CA' ]
Set trap:      true
sameItems:     false
Command Prompt output of TypeScript comparing arrays regardless of order with sorted copies and a Map counter, plus a Set duplicate trap
Both order-free checks pass, and the Set check wrongly matches lists with different duplicates.

That’s why I avoid a plain Set: it ignores duplicate counts. It’s great for getting distinct values from an array, not for counting.

Compare arrays of objects in TypeScript

Objects inside arrays have the same reference problem, so you need a deep comparison. Here’s a small deepEqual on API orders whose keys arrive in another order:

function deepEqual(a: unknown, b: unknown): boolean {
  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 ka = Object.keys(a);
  const kb = Object.keys(b);
  if (ka.length !== kb.length) return false;
  return ka.every((k) =>
    Object.hasOwn(b, k) && deepEqual((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k]));
}

interface Order { id: number; city: string; tags: string[] }

const saved: Order[] = [
  { id: 101, city: "Austin", tags: ["gift"] },
  { id: 102, city: "Denver", tags: [] },
];
const fromApi: Order[] = [
  { city: "Austin", id: 101, tags: ["gift"] },
  { id: 102, city: "Denver", tags: [] },
];

console.log("=== :        ", saved === fromApi);
console.log("JSON.stringify:", JSON.stringify(saved) === JSON.stringify(fromApi));
console.log("deepEqual:   ", deepEqual(saved, fromApi));
fromApi[1].tags.push("rush");
console.log("after change:", deepEqual(saved, fromApi));

Output:

=== :         false
JSON.stringify: false
deepEqual:    true
after change: false
Command Prompt output of TypeScript deepEqual comparing two arrays of order objects with keys in a different order
Only deepEqual says true, and it flips to false once a tag changes.

For dates or Map values, you’ll want Lodash isEqual. To copy data instead, see how to deep clone an object in TypeScript.

Want to keep going with arrays? Try these next:

Frequently asked questions

How do I check if two arrays are equal in TypeScript?

Check the lengths, then use a.every((v, i) => v === b[i]) inside a generic function.

Why is [1, 2] === [1, 2] false?

Each literal is a new object, and === compares references.

Is JSON.stringify safe for comparing arrays?

Only for flat, JSON-safe values. It treats null and undefined alike and depends on key order.

How do I compare arrays ignoring order?

Compare toSorted() copies, or count values with a Map. A Set alone misses duplicate counts.

How do I compare two arrays of objects?

Use a recursive deepEqual or Lodash isEqual.

Where is Array.prototype.every documented?