0
Why null==0 equals to false???
12 Answers
+ 5
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.
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 toĀ 0.
Equality Comparison: only callsĀ ToNumberĀ on Strings, Numbers, and Booleans.
+ 4
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.
+ 1
Because zero is type Number and null is type Null are 02 javascript objects very different
+ 1
No
+ 1
MĢ·oĢ·uĢ·lĢ·iĢ· š®š³ Thanks For Suggestion! š
+ 1
Null is abstract and non-zero (0==0)š¤
0
Franck Pertinent don't understood. Please explain well
0
Here equality check converts other types into number type before comparing.
So null is converted into 0.
Hence it is like 0==0.
Then it should be true, why false???
0
Type is compare into object types
String("12")!=12
0
Franck Pertinent what you are saying, bad Englishš¤
0
How equality works with null?
If we compare, let see what comes:
null > 0 // false (1)
null >= 0 // true (2)
null == 0 // false (3)
Now, in the (2), this statement means that null is equals to 0, as null is converted to number data type before comparing.
So null ==0 should be true according, my question is why result is false???
0
š”ļø Ulduz š”ļø
Really, this type of behaviour of null is really confusable. So it means that null can be converted only to undefined.
Well Thanks for your answers!š