0

Why this is false

Can someone explain why this is false?? let arr1 = [2, 4, 5, 'text']; let arr2 = [2, 4, 5, 'text']; console.log(arr1 == arr2); https://sololearn.com/compiler-playground/cgXvDUxkF870/?ref=app

28th Sep 2024, 5:36 PM
LORD HOSSEIN
LORD HOSSEIN - avatar
4 Antworten
+ 2
The code will output false because in JavaScript, arrays are compared by reference, not by value. Even though arr1 and arr2 contain the same elements, they are different objects in memory. To compare the contents, you need to loop through them and compare the various elements and compare the length of each list.
28th Sep 2024, 5:44 PM
Jerry Hobby
Jerry Hobby - avatar
28th Sep 2024, 10:14 PM
Sharpneli
Sharpneli - avatar
0
Jerry Hobby I thought that only happens in languages that don’t automatically detect variables, like Java, C#, and C++
29th Sep 2024, 11:08 AM
LORD HOSSEIN
LORD HOSSEIN - avatar
0
Sharpneli Thanks, but I already know that
29th Sep 2024, 11:11 AM
LORD HOSSEIN
LORD HOSSEIN - avatar