check if two arrays are equal in Javascript
Code snippet on how to check if two arrays are equal in Javascript
function arraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length)
return false;
for (var i = arr1.length; i--;) {
if (arr1[i] !== arr2[i])
return false;
}
return true;
}
This code snippet compares two JavaScript arrays to determine if they are equal. The first line checks if the length of the two arrays are equal. If the size is not the same, the two arrays are not equal and the function returns false. The for loop that follows loops through all the elements of the array and checks if each element is the same in the two arrays. If any of the elements are not the same, the function returns false. If all the elements are the same, the function returns true, signaling that the two arrays are equal.