Last modified: Jul 29, 2026
JS Compare Two Arrays Guide
Comparing two arrays in JavaScript is a common task. Arrays are objects, so you cannot use == or === to check if their contents are equal. This guide shows you simple, reliable ways to compare arrays, from basic loops to modern methods.
Why Direct Comparison Fails
In JavaScript, arrays are reference types. When you use ===, you are comparing memory references, not the values inside the arrays.
// Direct comparison does not work for content
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
console.log(arr1 === arr2); // false
false
Remember: Two arrays with identical elements are still different objects in memory.
Method 1: Comparing Arrays with a Loop
The most straightforward way is to write a loop that checks each element. This gives you full control.
function arraysEqual(a, b) {
// Check if lengths are the same
if (a.length !== b.length) return false;
// Compare each element
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
// Example usage
const arrA = [10, 20, 30];
const arrB = [10, 20, 30];
const arrC = [10, 20, 40];
console.log(arraysEqual(arrA, arrB)); // true
console.log(arraysEqual(arrA, arrC)); // false
true
false
This method works well for primitive values like numbers and strings. It is easy to read and debug.
Method 2: Using JSON.stringify
You can convert both arrays to JSON strings and compare them. This is a quick solution for simple arrays.
const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];
const arr3 = [1, 2, 4];
console.log(JSON.stringify(arr1) === JSON.stringify(arr2)); // true
console.log(JSON.stringify(arr1) === JSON.stringify(arr3)); // false
true
false
Caution: This method fails for arrays containing undefined, functions, or NaN. It also does not work if the order of properties in objects inside the array matters in a different way.
Method 3: Using Array.every and Array.includes
If you want to check if two arrays have the same elements regardless of order, use every and includes.
function sameElements(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
return arr1.every(item => arr2.includes(item));
}
const a = [1, 2, 3];
const b = [3, 1, 2];
console.log(sameElements(a, b)); // true
true
This approach is useful when order does not matter. However, it can be slow for large arrays because includes runs a loop inside every iteration.
Method 4: Deep Comparison for Nested Arrays
When arrays contain other arrays or objects, you need a recursive comparison. You can write a custom function or use a library like Lodash with its _.isEqual method.
// Simple recursive deep compare
function deepCompare(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i]) && Array.isArray(b[i])) {
if (!deepCompare(a[i], b[i])) return false;
} else if (a[i] !== b[i]) {
return false;
}
}
return true;
}
const nested1 = [[1, 2], [3, 4]];
const nested2 = [[1, 2], [3, 4]];
console.log(deepCompare(nested1, nested2)); // true
true
For production code, consider using a well-tested library. This saves time and reduces bugs.
Best Practices for Array Comparison
Follow these tips to write clean and reliable comparison code:
- Always check array lengths first. It is the fastest way to find a mismatch.
- Use
===for primitive values inside the loop. - For simple arrays,
JSON.stringifyis fine but be aware of its limitations. - For nested or complex data, use a recursive function or a library like Lodash.
- Keep performance in mind. A simple loop is often faster than converting to strings.
You can also learn more about working with arrays in our JavaScript Array Methods Guide and understand how to store data properly in the JavaScript Array Variables Guide.
Conclusion
Comparing two arrays in JavaScript requires understanding that you are comparing values, not references. Use loops for simple cases, JSON.stringify for quick checks, and deep comparison for nested structures. Always test your code with different data types and edge cases. With these techniques, you can confidently compare arrays in any project.