This question already has an answer here:
- How to compare arrays in JavaScript? 30 answers
Lets take two arrays
var apple = [1,2,3];
var bracket = [1,2,3];
and a string cat here with the same contents as the initial 2 arrays with no difference except comma (,)
var cat = "1,2,3";
Now lets test the equality between the 2 arrays with the following code.
if(apple == bracket){
console.log("Both are equal");
}
else{
console.log("Both are not equal");
}
Everyone including me would have guessed it would print Both are equal
.But the opposite is true.It prints Both are not equal
.How is this possible.
Here is more surprice
if(apple == cat){
console.log("Both are equal");
}
else{
console.log("Both are not equal");
}
Here also the otherway the output comes,it would print Both are equal
.
Aucun commentaire:
Enregistrer un commentaire