Guides

How to compare two arrays in JavaScript — and why [1, 2] doesn't equal [1, 2]

Illustration: two trays of beads side by side, a magnifying glass checking them one by one

Open your browser console and type:

[1, 2] === [1, 2]   // false

Two identical lists — and JavaScript says "not equal". You ask an AI: "check whether the list changed". It writes JSON.stringify(a) === JSON.stringify(b). Works… until a NaN ends up in the array. We checked: JSON.stringify([NaN]) and JSON.stringify([null]) produce the same string [null]. Different arrays — "equal".

JavaScript has no built-in "compare arrays" function. But it's easy to build one properly. Let's go step by step.

Why === doesn't work

An array is an object. And === compares objects by reference: "is this the very same box?", not "do the boxes hold the same things?". Every [1, 2] is a new box in memory. Different references — the answer is false.

=== returns true only when both variables point to one array:

const a = [1, 2]
const b = a
a === b   // true — it's one box

So you have to compare the contents. Exactly how depends on what "the same" means to you.

Step by step: how to compare properly

  1. Decide what "equal" means. Three different questions: same order? same items in any order? objects inside, or simple values? The code depends on the answer. Ask yourself before you ask the AI.

  2. Same order, simple values. Compare the length, then each item by index:

    const isSame = (a, b) =>
      a.length === b.length && a.every((x, i) => Object.is(x, b[i]))
    
    isSame([1, 2, 3], [1, 2, 3])   // true
    isSame([1, 2, 3], [3, 2, 1])   // false
    

    The length check is a must: without it [1, 2] and [1, 2, 3] come out "equal". And Object.is instead of === is there for one case: NaN === NaN is false, while Object.is(NaN, NaN) is true.

  3. Any order. Sort copies and compare them with the function from step 2:

    const isSameAnyOrder = (a, b) => isSame(a.toSorted(), b.toSorted())
    
    isSameAnyOrder([3, 1, 2], [1, 2, 3])   // true
    isSameAnyOrder([1, 1, 2], [1, 2, 2])   // false — correct
    

    toSorted() returns a new array and leaves the original alone (it's in all current browsers and Node 20+). Plain sort() would reshuffle the items right inside your data.

    Careful with the popular tip "compare with a Set". A Set drops duplicates. To it, [1, 1, 2] and [1, 2, 2] are the same: both become 1, 2. We checked — a Set comparison says "equal".

  4. Objects inside. Step 2 won't save you here: { id: 1 } and { id: 1 } are also different objects, the same reference story. Two ways:

    • Compare by key. If the objects have an id, compare the id lists: isSame(a.map(u => u.id), b.map(u => u.id)). Fast and clear.
    • Deep comparison. Node.js has a ready-made function: util.isDeepStrictEqual(a, b). It looks inside objects and handles NaN correctly. In the browser, lodash's isEqual does the same. In tests — expect(a).toEqual(b).

    And for objects JSON.stringify also trips over key order: { a: 1, b: 2 } and { b: 2, a: 1 } turn into different strings.

  5. Find what's different. Often you need the difference itself, not a yes/no:

    const added   = b.filter(x => !a.includes(x))   // appeared
    const removed = a.filter(x => !b.includes(x))   // disappeared
    

    For big lists (thousands of items), build new Set(a) first and check with has — it's many times faster.

What you end up with

You have two small functions, isSame and isSameAnyOrder, and you know which to use when. A typical case is a "Save" button that lights up only when something really changed:

saveButton.disabled = isSame(savedTags, currentTags)

No more "saved" when nothing changed. And no false "equal" because of NaN or duplicates.

Can I compare arrays with toString?

Better not. [1, 2].toString() gives the string 1,2 — exactly the same as an array holding one string, ["1,2"]. Different arrays, identical text.

Can I use a for loop instead of every?

You can — it means the same thing. every is just shorter and stops on its own at the first mismatch. More on choosing between loops and array methods in for loop or map.

How do I ask AI to compare arrays?

Say what "equal" means to you. Otherwise the model reaches for JSON.stringify out of habit.

Weak promptcheck if two arrays are the same
Strong promptWrite a function isSameTags(a, b) for two arrays of tag strings. Order does not matter, duplicates do: [x, x, y] and [x, y, y] are different. Do not use JSON.stringify or Set. Do not modify the original arrays. Add three example calls with the expected result.
Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →