Array Matching in Javscript

Array are objects in JavaScript. You can look at like this
var arr1=[1,2,3];
console.log(typeof arr1); //"object"
In javascript, comparing objects (incl. arrays) against one and other they are only compared by reference - i.e. arr1 == arr2 will only be true if they are the same array. Two separate arrays/objects that look the same are not compared by value.
To make it clearer; only the primitive values can be compared for equality - i.e. boolean, string, number. The special values null and undefined also act this way.
var arr1 = [1];
var arr2 = [1];
var arr3 = arr1;
var val1 = 1;

arr1 == arr2 // false
arr1 == arr3 // true
arr1[0] == val1; // true
For primitives values array matching, you can use the trick
JSON.stringify(arr1) === JSON.stringify(arr2)

Share this post