Javascript: Check if two arrays are equal

While working in javascript arrays, often there is a requirement to compare two arrays and check if two arrays are equal. This article demonstrates easy ways to check if two arrays are equal using different methods and example illustrations.

We will cover both strict comparisons as well as comparing two arrays irrespective of the order of elements.

Table of Contents:

Check if two arrays are equal using every()

Javascript’s every() method will test if all elements pass the test implemented by the provided function. If all the elements pass the test, true is returned. Else false is returned.

Example:-

Check if arrays array2 and array3 are equal to array1

  • array1 = [1,2,”Hello”,7]
  • array2 = [“Hello”,7,2,1]
  • array3 = [1,2,”Hello”,7]

Code:-

function checkArrayEquality(_array1, _array2) 
{
    return _array1.length === _array2.length && 
    _array1.every( function(_array1_i ,i) 
     { 
     return _array1_i === _array2[i] 
      } )  
}
//usage
let array1 = [1,2,"Hello",7] 
let array2 = ["Hello",7,2,1] 
let array3 = [1,2,"Hello",7] 
console.log(checkArrayEquality(array1, array2));
console.log(checkArrayEquality(array1, array3));

Output:-

false
true

Explanation:-

  • Here, two conditions are checked with an && operator. The first is to check if the length of both arrays is equal.
  • The second condition applies the every() method to all elements of the array. Within the every() method, each element of _array1 gets compared with the corresponding element of the other array _array2.
  • If all the elements are equal, true is returned, else false is returned.

Note that one can use every() method from javascript version 1.6 onwards.

Check if two arrays are equal using iteration

Example:-

Check if arrays array2 and array3 are equal to array1

  • array1 = [1,2,”Hello”,7]
  • array2 = [“Hello”,7,2,1]
  • array3 = [1,2,”Hello”,7]

Code:-

  function checkArrayEquality(_array1, _array2) {
    if (_array1 === null || _array2 === null) 
        return false;
    if (_array1.length !== _array2.length)
        return false;
    for (var i = 0; i < _array1.length; ++i) 
    {
         if (_array1[i] !== _array2[i]) 
         return false;
    }
    return true;
  }
//usage
let array1 = [1,2,"Hello",7] 
let array2 = ["Hello",7,2,1] 
let array3 = [1,2,"Hello", 7] 
console.log(checkArrayEquality(array1,array2));
console.log(checkArrayEquality(array1,array3));

Output:-

false
true

Explanation:-

  • The first if statement verifies that none of the array is null. If so, false is returned.
  • The second if statement will test if the length of both the arrays are equal. If so, false is returned.
  • For loop is run to compare the elements of both the arrays at respective indexes. If any of the array elements are unequal, false is returned.
  • Else, true is returned.

Check if two arrays are equal using stringify()

The JSON.stringify() method will convert the javascript value or the object to a JSON string.

Example:-

Check if arrays array2 and array3 are equal to array1

  • array1 = [1,2,”Hello”,7]
  • array2 = [“Hello”,7,2,1]
  • array3 = [1,2,”Hello”,7]

Code:-

function checkArrayEquality(_array1, _array2) 
 {
   return JSON.stringify(_array1) === JSON.stringify(_array2) ;
 }
//usage
let array1 = [1,2,"Hello",7] 
let array2 = ["Hello",7,2,1] 
let array3 = [1,2,"Hello", 7] 
console.log(checkArrayEquality(array1,array2));
console.log(checkArrayEquality(array1,array3));

Output:-

false
true

Explanation:-

  • Here both the values are converted to JSON string and then compared. If the elements of the array are equal and in the same order, true is returned. Else false is returned.

Note that stringify() method converts undefined to null.

Check if two arrays are equal irrespective of their order of elements

Example:-

Check if arrays array2 and array3 are equal to array1

  • array1 = [1,2,”Hello”,7]
  • array2 = [“Hello”,7,2,1]
  • array3 = [1,2,”Hello”,7]

Code:-

function checkArrayEquality(_array1, _array2)
 {
    if (_array1 == null || _array2 == null) 
        return false;
    if (_array1.length !== _array2.length) 
        return false;
     _array1.sort();
     _array2.sort();
    for (var i = 0; i < _array1.length; ++i) 
    {
      if (_array1[i] !== _array2[i]) 
        return false;
    }   
    return true;
  }
//usage
let array1 = [1,2,"Hello",7] 
let array2 = ["Hello",7,2,1] 
let array3 = [1,2,"Hello", 7] 
console.log(checkArrayEquality(array1,array2));
console.log(checkArrayEquality(array1,array3));

Output:-

true
true

Explanation:-

  • Here, we are sorting the elements of both the arrays and then comparing each element of one array with another.

Note that using the sort() method modifies the array.

Read More:

I hope this article helped you to check if two arrays are equal in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top