JavaScript Code Explanation | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

JavaScript Code Explanation

var a = null; var b = {}; var c = a || b; alert(c); The answer of this challenge question is [Object Object] but how?

2nd Oct 2023, 1:21 AM
Umer Khan
Umer Khan - avatar
4 Réponses
2nd Oct 2023, 2:48 AM
Aaron Lee
Aaron Lee - avatar
+ 2
The way I understand it is: when you perform the above comparison "a || b" it is coercing "null" and "{}" into booleans. when coerced, null evaluates as false and objects as true. so "{}" wins the comparison battle and becomes the value set to var c.
2nd Oct 2023, 2:02 AM
Aaron Lee
Aaron Lee - avatar
+ 2
The answer is [Object Object] twice But I don't know why?
2nd Oct 2023, 2:09 AM
Umer Khan
Umer Khan - avatar
+ 2
Thanks Aaron
2nd Oct 2023, 4:13 AM
Umer Khan
Umer Khan - avatar