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"); }

12th Apr 2022, 12:07 PM
Sandeepa De Silva
4 Réponses
+ 5
Sandeepa De Silva There is a good discussion of this array subject here : https://masteringjs.io/tutorials/fundamentals/compare-arrays
12th Apr 2022, 2:41 PM
Paul K Sadler
Paul K Sadler - avatar
+ 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.
12th Apr 2022, 9:50 PM
Sherief Ismail
Sherief Ismail - avatar
+ 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')
12th Apr 2022, 12:39 PM
A͢J
A͢J - avatar
+ 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’); }
14th Apr 2022, 12:06 PM
ITDW