Comparing two arrays in JavaScript or jQuery might seem simple, but there’s more to it than just using ==
or ===
. Arrays are reference types, meaning even if two arrays contain the same values, they aren’t equal unless they reference the exact same object.
If you’re checking whether two arrays contain the same values in the same order, you need to compare them element by element.
In this post, we’ll explore how to compare arrays accurately using both JavaScript and jQuery. You’ll learn where built-in methods fall short, and how to handle both primitive values and complex structures. If you’re new to arrays, check out our complete guide to JavaScript arrays.
Comparing Arrays in JavaScript
JavaScript offers several practical ways to compare arrays, depending on whether you’re working with primitive values, objects, or care about order. These techniques are also helpful when you’re filtering arrays or performing data validation.
Why == and === Don’t Work
Let’s start with an example that shows why reference comparison fails:
const a = [1, 2, 3];
const b = [1, 2, 3];
console.log(a === b); // false
Although both arrays have identical values, they are two different objects in memory. That’s why this returns false
.
Compare Arrays with Length and every()
The most common way to compare arrays in JavaScript is to:
- Check if their lengths are equal
- Compare each element one by one using
every()
function arraysEqual(a, b) {
return a.length === b.length && a.every((val, index) => val === b[index]);
}
const x = [1, 2, 3];
const y = [1, 2, 3];
console.log(arraysEqual(x, y)); // true
This method works well for flat arrays containing primitive values. For more on working with array methods like every()
, visit our array guide.
Quick Comparison Using JSON.stringify()
Another simple way to compare flat arrays is by converting them to strings:
const a = [1, 2, 3];
const b = [1, 2, 3];
console.log(JSON.stringify(a) === JSON.stringify(b)); // true
This approach works only when:
- The arrays are flat (no nested objects)
- The order of elements matters
Compare Arrays Without Considering Order
If the order of elements doesn’t matter, you can sort both arrays before comparison:
function unorderedEqual(a, b) {
return a.length === b.length &&
a.sort().every((val, index) => val === b.sort()[index]);
}
console.log(unorderedEqual([1, 2, 3], [3, 2, 1])); // true
Sorting arrays is a common operation—learn more about it in our guide to sorting arrays in JavaScript.
Just remember that sort()
mutates the original array, so make a copy if you want to keep the original order.
Comparing Arrays of Objects
For basic comparison of object arrays, you can also use JSON.stringify()
— as long as the object keys are in the same order:
const a = [{ id: 1 }, { id: 2 }];
const b = [{ id: 1 }, { id: 2 }];
console.log(JSON.stringify(a) === JSON.stringify(b)); // true
Use this approach only when key order is guaranteed to match between objects.
Comparing Arrays in jQuery
jQuery doesn’t provide a built-in method for array comparison, but you can still achieve it by combining jQuery helpers like $.each()
with standard JavaScript logic. If you’re already using jQuery for DOM interaction or event handling, these methods will integrate smoothly into your code.
jQuery Approach with $.each and Manual Checks
Here’s how you can compare two arrays by looping through each item and checking values manually:
function arraysEqual(a, b) {
if (a.length !== b.length) return false;
var equal = true;
$.each(a, function(index, value) {
if (value !== b[index]) {
equal = false;
return false; // break loop
}
});
return equal;
}
var a = [1, 2, 3];
var b = [1, 2, 3];
console.log(arraysEqual(a, b)); // true
Ignoring Order in jQuery
If the order of elements doesn’t matter, sort both arrays before comparing them:
function unorderedEqual(a, b) {
if (a.length !== b.length) return false;
a.sort();
b.sort();
var equal = true;
$.each(a, function(index, value) {
if (value !== b[index]) {
equal = false;
return false;
}
});
return equal;
}
console.log(unorderedEqual([3, 2, 1], [1, 2, 3])); // true
Comparing Arrays of Objects in jQuery
For comparing arrays of objects, you can still use JSON.stringify()
in jQuery-based projects:
var a = [{ id: 1 }, { id: 2 }];
var b = [{ id: 1 }, { id: 2 }];
console.log(JSON.stringify(a) === JSON.stringify(b)); // true
This works only if the structure and key order are identical.
Final Thoughts
Comparing arrays correctly takes more than just using ==
or ===
. In JavaScript, use every()
or JSON.stringify()
for flat arrays, and sort if you don’t care about order. In jQuery, you can achieve similar results with $.each()
and manual logic.
Want to go further? Read our post on removing duplicates from arrays using JS or jQuery.