How to compare arrays in JavaScript?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Comparing arrays in JavaScript is tricky because arrays are objects, and object equality is based on references, not contents. Two arrays that look identical can still compare as different values if they are stored at different memory locations.
Reference Equality Versus Value Equality
This is the first thing to understand:
a === b is false because they are different array objects. a === c is true because both variables point to the same array.
If what you really want is content equality, you need custom comparison logic.
Comparing Flat Arrays in Order
For arrays of primitive values where order matters, compare lengths first and then compare each element.
This is the right solution for many day-to-day cases because it is explicit and fast.
Using every
If you prefer a more declarative style, Array.prototype.every gives the same behavior with less loop syntax:
This still performs an ordered, shallow comparison. Nested arrays and objects need more than ===.
Deep Comparison for Nested Arrays
When arrays can contain other arrays or plain objects, you need recursion. A simple deep-equality helper might look like this:
This kind of helper is useful when you control the data shape and do not want an external dependency.
Is JSON.stringify Good Enough
JSON.stringify is tempting because it is short:
It can work for simple arrays of primitives, but it is fragile as a general solution. It depends on serialization details, drops unsupported values such as functions, and does not express your intent clearly. It is best treated as a quick debugging shortcut, not a reusable equality strategy.
When Order Does Not Matter
Sometimes you are not comparing arrays as ordered sequences. You are comparing them as bags or sets of values. In that case, sort first or use frequency counting.
That is a different question from ordered equality, so the comparison rule should be chosen deliberately.
Common Pitfalls
The biggest pitfall is using === and expecting content comparison. In JavaScript, that only checks whether both variables reference the same array.
Another problem is forgetting to compare lengths first. Without that check, some implementations accidentally report equality for arrays that share a prefix but have different sizes.
JSON.stringify also causes subtle bugs when array elements are objects whose property order or serializability is not guaranteed. It looks convenient, but it is often the wrong abstraction.
Finally, be explicit about whether order matters. Comparing [1, 2, 3] and [3, 2, 1] should return false for sequence equality and true only for set-like equality.
Summary
- '
===compares array references, not contents.' - For flat ordered arrays, compare lengths and corresponding elements.
- '
everyis a concise way to implement shallow ordered equality.' - Nested arrays or objects require deep comparison logic or a library.
- Decide whether you want sequence equality or set-like equality before choosing an approach.
Related reading
- How to compare two Dictionaries in C
- How to compare two ListString to each other?
- How to compress pointer ? eg. arbitrary bit pointer
- How to compute intersection of N sorted sets?
- how to connect to rabbitmq using javascript without nodejs
- How to consume just one message from rabbit mq on nodejs
- How to concatenate join items in a list to a single string
- How to concatenate two dictionaries to create a new one?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.