what is the output of this code? Here is asking for the argument.. how can it be false the first one? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 2

what is the output of this code? Here is asking for the argument.. how can it be false the first one?

what is the output of this code? var arr1 = [1,2,3]; var arr2 = [1,2,3]; var arr3 = arr1; console.log(arr1==arr2); console.log(arr1==arr3); /* Showing the result as 'false true', why?

17th Dec 2022, 11:00 AM
Sony
Sony - avatar
3 Antworten
+ 6
assuming you are talking about Javascript, you should mostly use === Array equality is tricky, though... https://flexiple.com/javascript/javascript-array-equality/ https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript
17th Dec 2022, 11:06 AM
Bob_Li
Bob_Li - avatar
+ 5
In the first comparison, arr1 and arr2 are being compared using the == operator, which compares their values rather than their references. Since arr1 and arr2 are distinct arrays with the same elements, they are considered equal in value, but not equal in reference. Therefore, the comparison evaluates to false. In the second comparison, arr1 and arr3 are also being compared using the == operator. However, in this case arr3 is assigned the value of arr1, so they both refer to the same array in memory. Therefore, the comparison evaluates to true.
17th Dec 2022, 7:03 PM
Sadaam Linux
Sadaam Linux - avatar
+ 3
From what I understood: arr1 == arr3 will return true because arr3 and arr1 points to same object And arr1 == arr2 returns false because they point to different objects (even though both arrays have the same content, still they're different)
17th Dec 2022, 11:37 AM
Roopesh
Roopesh - avatar