array compare detailed 1

array compare detailed

var a1 = [1,2,3];
var a2 = [1,2,3];
console.log(a1==a2);    // Returns false
console.log(JSON.stringify(a1)==JSON.stringify(a2));    // Returns true

Here is what the above code is Doing:
1. We are creating two arrays a1 and a2.
2. We are comparing the two arrays using == operator.
3. We are comparing the two arrays using JSON.stringify() method.

The == operator compares the references of the two arrays. Since the references are different, it returns false.

The JSON.stringify() method converts the array into a string and then compares the strings. Since the strings are same, it returns true.

Similar Posts