Program to test the equality of two arrays - JavaScript



We are required to write a JavaScript function that takes in two arrays of literals and checks the corresponding elements of the array and it should return true if all the corresponding elements of the array are equal otherwise it should return false.

Let’s write the code for this function −

Example

Following is the code −

const arr1 = [1, 4, 5, 3, 5, 6];
const arr2 = [1, 4, 5, 2, 5, 6];
const areEqual = (first, second) => {
   if(first.length !== second.length){
      return false;
   };
   for(let i = 0; i < first.length; i++){
      if(first[i] === second[i]){
         continue;
      }
      return false;
   };
   return true;
};
console.log(areEqual(arr1, arr2));

Output

Following is the output in the console −

false

Advertisements