Why `null >= 0 && null <= 0` but not `null == 0`?
I'd like to extend the question to further improve visibility of the problem:
null >= 0; //true
null <= 0; //true
null == 0; //false
null > 0; //false
null < 0; //false
It just makes no sense. Like human languages, these things need be learned by heart.
console.log( null > 0 ); // (1) false
console.log( null == 0 ); // (2) false
console.log( null >= 0 ); // (3) true
Mathematically, that’s strange. The last result states that "null is greater than or equal to zero", so in one of the comparisons above it must be true, but they are both false.
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
.
JavaScript has both strict and type–converting comparisons
null >= 0;
is true
but
(null==0)||(null>0)
is false
null <= 0;
is true but (null==0)||(null<0)
is false
"" >= 0
is also true
For relational abstract comparisons (<= , >=), the operands are first converted to primitives, then to the same type, before comparison.
typeof null returns "object"
When type is object javascript tries to stringify the object (i.e null) the following steps are taken (ECMAScript 2015):
- If
PreferredType
was not passed, lethint
be "default". - Else if
PreferredType
ishint
String, lethint
be "string". - Else
PreferredType
ishint
Number, lethint
be "number". - Let
exoticToPrim
beGetMethod(input, @@toPrimitive)
. ReturnIfAbrupt(exoticToPrim)
.- If
exoticToPrim
is not undefined, then
a) Let result beCall(exoticToPrim, input, «hint»)
.
b)ReturnIfAbrupt(result)
.
c) IfType(result)
is not Object, return result.
d) Throw a TypeError exception. - If
hint
is "default", lethint
be "number". - Return
OrdinaryToPrimitive(input,hint)
.
The allowed values for hint are "default", "number", and "string". Date objects, are unique among built-in ECMAScript object in that they treat "default" as being equivalent to "string". All other built-in ECMAScript objects treat "default" as being equivalent to "number". (ECMAScript 20.3.4.45)
So I think null
converts to 0.
Your real question seem to be:
Why:
null >= 0; // true
But:
null == 0; // false
What really happens is that the Greater-than-or-equal Operator (>=
), performs type coercion (ToPrimitive
), with a hint type of Number
, actually all the relational operators have this behavior.
null
is treated in a special way by the Equals Operator (==
). In a brief, it only coerces to undefined
:
null == null; // true
null == undefined; // true
Value such as false
, ''
, '0'
, and []
are subject to numeric type coercion, all of them coerce to zero.
You can see the inner details of this process in the The Abstract Equality Comparison Algorithm and The Abstract Relational Comparison Algorithm.
In Summary:
Relational Comparison: if both values are not type String,
ToNumber
is called on both. This is the same as adding a+
in front, which for null coerces to0
.Equality Comparison: only calls
ToNumber
on Strings, Numbers, and Booleans.