0

Null & 0 value in js

Why : 0 >= null is true 0 <= null Is true But ! 0 == null Is false 0 < null Is false 0 > null Is false

19th Mar 2024, 5:30 PM
Yasin Rahnaward
Yasin Rahnaward - avatar
3 Antworten
+ 4
"The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, treating it as 0. That’s why (3) null >= 0 is true and (1) null > 0 is false. On the other hand, the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don’t equal anything else. That’s why (2) null == 0 is false." https://javascript.info/comparison
19th Mar 2024, 6:20 PM
Lisa
Lisa - avatar
+ 2
There is not much point in asking WHY. Javascript has a lot of wacky behavior with implicit conversions and coercions, that affect these operators. You just have to accept that it works like this. Because the language was designed this way. :)
19th Mar 2024, 6:33 PM
Tibor Santa
Tibor Santa - avatar
+ 2
in addition to Lisa JavaScript returns undefined for null and undefined in loosely Equal (==) https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islooselyequal
19th Mar 2024, 7:22 PM
Yasin Rahnaward
Yasin Rahnaward - avatar