0
Array equality in java script
Why following if statement is not considered as true one. Var a= array new("DaCh"); Var b=array new("DaCh"); If (a==b){ Alert("true"); }
4 Réponses
+ 5
Sandeepa De Silva
There is a good discussion of this array subject here : https://masteringjs.io/tutorials/fundamentals/compare-arrays
+ 5
In general if you make a logical comparison (==)between any two objects in JS, the result would always be they are not equal . At the end of the day and from JS standpoint, arrays are just some other kind of object so the two arrays comparison result will never be true even though they both seem identical.
+ 4
Sandeepa De Silva
Because you are comparing arrays which cannot be equal
also syntax is wrong (array new())
Here is right syntax:
var a = new Array('abc')
var b = new Array('abc')
+ 1
1st, using new Array() is actually slower than brackets []
You could compare arrays with the join() method and compare the string
var a = [‘DaCh’];
var b = [‘DaCh’];
if (a.join(‘’) == b.join(‘’)) {
alert(‘true’);
}