Check for array equality in JavaScript
This post will discuss how to check for array equality in JavaScript. In other words, check if two arrays have the same elements in the same order.
Checking for array equality in JavaScript is not a straightforward task, as there are different ways to compare two arrays and different criteria to define equality. Depending on the use case, we may want to check if two arrays have the same reference, the same length, the same elements in the same order, the same elements in any order, or the same elements with deep equality (recursively comparing nested objects and arrays).
To check for array equality in JavaScript, we need to compare the lengths and the elements of the arrays at the same positions. However, we cannot use the strict equality operator (===) directly on the arrays, because it will only compare the references of the arrays, not their values. Meaning it will return true only when arrays point to the same object in memory, and will return false if two arrays have different references, even if they have the same elements. Here’s an example:
|
1 2 3 4 5 6 |
const arr1 = [1, 2, 3]; const arr2 = [1, 2, 3]; const arr3 = arr1; console.log(arr1 === arr2); // false (different references) console.log(arr1 === arr3); // true (same reference) |
Therefore, we need to use some functions and techniques that can help us check for array equality in JavaScript. Here are some of the common ones, each with its own advantages and disadvantages:
1. Using Array.every() function
The Array.every() function tests whether all elements in the array pass a test implemented by a provided function. We can use this function to check if each element in the first array is strictly equal to the corresponding element in the second array, using the index parameter of the callback function. Here’s an example:
|
1 2 3 4 5 6 7 |
let arr1 = [1, 2, 3]; let arr2 = [1, 2, 3]; let isEqual = (arr1.length === arr2.length) && arr1.every((value, index) => value === arr2[index]); console.log(isEqual); // true |
2. Using JSON.stringify() function
The JSON.stringify() function converts an array into a JSON string, which can then be compared using the strict equality operator ===. This function works well for arrays of numbers or strings, but it may not work for arrays of objects or other types. Here’s an example:
|
1 2 3 4 5 6 |
let arr1 = [1, 2, 3]; let arr2 = [1, 2, 3]; let isEqual = JSON.stringify(arr1) === JSON.stringify(arr2); console.log(isEqual); // true |
3. Using Custom Recursive Function
This function defines a custom function that can check for array equality by comparing the elements of the arrays recursively. The function uses a base case to check if the arrays are empty or have different lengths, and then compares the first elements of the arrays using the strict equality operator (===). If they are equal, it calls itself on the rest of the arrays using the slice() function. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
let arr1 = [1, 2, 3]; let arr2 = [1, 2, 3]; // a custom function that can check for array equality function isEqual(arr1, arr2) { // if they have different lengths, they are not equal if (arr1.length !== arr2.length) { return false; } // if they are both empty, they are equal if (arr1.length === 0) { return true; } // if their first elements are not equal, they are not equal if (arr1[0] !== arr2[0]) { return false; } // otherwise, recursively check the rest of the arrays return isEqual(arr1.slice(1), arr2.slice(1)); } console.log(isEqual(arr1, arr2)); // true |
That’s all about checking for array equality in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)