OneCompiler

[JavaScript] How to compare arrays?

I want to compare two arrays and see if they are equal, how can I do that in Javascript?

1 Answer

4 years ago by

You can use any of the below methods to compare two arrays in Javascript

Method: 1

(arr1.length === arr2.length) && arr1.every((ele, index) => ele === arr2[index])

Method: 2

JSON.stringify(arr1)===JSON.stringify(arr2)

Try running the result here : https://onecompiler.com/javascript/3xmzttmj3

4 years ago by Meera