Check Two Arrays Have the Same Elements

If you have been learning JavaScript , you might have came across == or === operator. It does not work as usual with arrays. Because arrays in JavaScript are objects. You won’t be able to find out if two arrays have equal elements at corresponding index values using these operators. In JavaScript, the == operator is used for equality comparison, but when comparing arrays, it checks for reference equality, not structural equality. This means that it checks if both arrays reference the same object in memory, not if they have the same elements.

let arr1=[1, 2, 3, 4] ; 
let arr2=[1, 2, 3, 4];

console.log(arr1 ==arr2); // false
console.log(arr1 === arr2); // false

console.log(JSON.stringify(arr1)); // '[1,2,3,4]'
console.log(JSON.stringify(arr2)); // '[1,2,3,4]'

console.log(JSON.stringify(arr1) == JSON.stringify(arr2)); // true

To find out if two arrays have the same elements , we can use a number of methods. You can compare if every element in one array ,is present in other array and lengths of both arrays are equal. Or, you can find out by first converting array to string .

Below are a few ways to compare arrays for equality:

Using a loop:

function areArraysEqual(arr1, arr2) {
  if (arr1.length !== arr2.length) {
    return false;
  }
  for (let i = 0; i < arr1.length; i++) {
    if (arr1[i] !== arr2[i]) {
      return false;
    }
  }
  return true;
}
const array1 = [1, 2, 3];
const array2 = [1, 2, 3];
console.log(areArraysEqual(array1, array2)); // true

Using JSON.stringify:

const array1 = [1, 2, 3];
const array2 = [1, 2, 3];
console.log(JSON.stringify(array1) === JSON.stringify(array2)); // true

Note: This approach works well for simple arrays of primitive types but might not be reliable for arrays with nested structures or functions. However you can first flatten array then stringify if array has nested structure or functions. Learn how to flatten array here.

Using array methods:

If both arrays have same length and for every element in array1 there is corresponding element value in array2 at the same index.

const array1 = [1, 2, 3];
const array2 = [1, 2, 3];
console.log( array1.length === array2.length && array1.every((value, index) => value === array2[index]) ); // true

If there exists at least one pair of corresponding elements in array1 and array2 that are not equal, it implies that the arrays as a whole do not have equal elements. You can use the some() method to check for this condition:

const array1 = [1, 2, 3];
const array2 = [1, 2, 4];

const arraysAreEqual = array1.length === array2.length && !array1.some((value, index) => value !== array2[index]);

console.log(arraysAreEqual); // false

In this example, array1.some((value, index) => value !== array2[index]) checks if there is at least one pair of corresponding elements that are not equal. The ! operator negates the result, so the overall condition checks if all pairs are equal.

You can also check using if both arrays have equal length and every element in array1 is included in array2.

function areArraysEqual(arr1, arr2) {
  return (
    arr1.length === arr2.length &&
    arr1.every((element) => arr2.includes(element))
  );
}

// Example usage:
const array1 = [1, 2, 3, 4, 5];
const array2 = [5, 4, 3, 2, 1];

if (areArraysEqual(array1, array2)) {
  console.log("Arrays have equal elements.");
} else {
  console.log("Arrays  do not have equal elements.");
}

You can learn various array methods in JavaScript here.

Don't Miss Out! Subscribe to Read Our Latest Blogs.

If you found this blog helpful, share it on social media.

Subscription form (#5)

Pin It on Pinterest

Scroll to Top