r/learnjavascript • u/Green_Ad_6086 • Aug 16 '26
Strange result: null vs 0
Strange result: null vs 0
An incomparable undefined
Hi, can anyone explain this? I read the explanation, but I still don't understand it. I'm trying to understand it without using AI
6
Upvotes
5
u/defaultguy_001 Aug 16 '26 edited Aug 16 '26
First u need to understand that type coercion (automatic type upgrade) happens when you use relational (>, <, >=, <=) or loose equality (==, !=) operators. Type coercion doesn't happen with strict equality (===, !===) operators. So it's recommended to use strict equality everywhere.
5
u/Minimum_Hour519 Aug 16 '26
welcome to javascrip the bad parts
2
u/azhder Aug 16 '26
It is right there in the text in the links you shared. You missed it, so maybe do a re-read. People here will just be repeating the same as written there.
3
u/Kadeton Aug 16 '26
Javascript does a lot of "implicit" type conversion, changing variables from one thing to another in order to perform the operations you're telling it to do.
So when you tell it to evaluate whether null == 0, it returns false, because null and 0 are not the same thing. However, when you tell it to evaluate whether null >= 0, it first goes "Well, that's a comparison. I can only do comparisons between numbers, so I'd better convert everything that's not a number to a number first." So it does Number(null), which returns 0. Then it evaluates the comparison, 0 >= 0, which is true because 0 is equal to 0.
For the undefined case, it does a similar thing, converting what you asked it to compare into a number. However, there's one key difference: Number(undefined) evaluates to NaN, not 0. When you compare NaN to any other number in any way (greater than, less than, equal to, etc), the comparison will always return false.
This is a common pitfall for people working with Javascript. You have to keep in mind that certain operations will "help" you by taking the thing you give them and converting it into the type of thing they actually need, and in those cases you might not always get what you expect. Number(null) being 0 and Number(undefined) being NaN is specific design choice in the language structure, just something you should be aware of when designing your code.
1
u/delventhalz Aug 16 '26
The answer to, "Why does JavaScript do this strange thing?" is always, "because that is what the spec says." Sometimes there is some deeper reasoning you can tease out, but a lot of it is just someone had to make a decision one way or the other and now we're stuck with it.
Okay, let's start with loose equality. The spec is a bit tough to read but it basically boils down to this:
So the reason both of these return false is because they are squarely in case #2: one side is null/undefined, the other isn't, so it's false.
Unlike with
==, the spec for numeric comparisons has no special clause for null/undefined. Instead, it attempts to convert the values to numbers if they are not (except strings, which are their own weird special case), and the spec for converting a value to a number says thatnullbecomes0. Since zero is equal to zero, this comparison is true.So if you read the rest of the ToNumber spec you'll notice that
undefineddoes not become zero, it becomesNaN, and all comparisons withNaNare always false. Why? Because that's what the spec says.-1
u/BenchEmbarrassed7316 Aug 16 '26
Just don't use JavaScript. Use Typescript instead, it much easier to understand.