Skip to content

Instantly share code, notes, and snippets.

@Nicknyr
Last active December 9, 2017 23:36
Show Gist options
  • Save Nicknyr/3dc9ccb7129272bf8dc0f76c712f5906 to your computer and use it in GitHub Desktop.
Save Nicknyr/3dc9ccb7129272bf8dc0f76c712f5906 to your computer and use it in GitHub Desktop.
Javascript: Use .every() function to compare two arrays and determine if they have the same elements in the same order
// Compare two arrays and determine if they have the same values in the same order
var compare = arr1.length == arr2.length &&
arr1.every(function(element, index){
return element == arr2[index];
});
if(compare) {
// Arrays match, do this
}
else {
// Arrays don't match, do this
}
@0Prime
Copy link

0Prime commented Dec 9, 2017

console.log(compare([1], ['1'])) // true

Is this intended behavior? If not, consider using === inside every function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment