Have been bug testing for about 2 hours now only to realise that if two arrays are the same. The logical operator ==
and ===
say it isn't.
Firstly, why. Secondly, how can I get this to be true?
Have been bug testing for about 2 hours now only to realise that if two arrays are the same. The logical operator ==
and ===
say it isn't.
Firstly, why. Secondly, how can I get this to be true?
Array is a reference data type, and can never be equal. You can check and compare the data inside both arrays, without shell
Try this:
JSON.stringify(array1) == JSON.stringify(array2)
You're using logical comparisons between values, not for arrays/objects. Alternatively you can:
if (array1.length == array2.length){
for(var i = 0; i <= array1.length; i++){
if(array1[i] != array2[i]){
return false;
}
}
return true;
} else {
return false;
}