Why null==0 equals to false??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why null==0 equals to false???

19th Aug 2020, 10:39 AM
Himanshu Rai
Himanshu Rai - avatar
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.
19th Aug 2020, 5:03 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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.
19th Aug 2020, 5:06 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Because zero is type Number and null is type Null are 02 javascript objects very different
19th Aug 2020, 10:49 AM
HEUBA BATOMEN Franck Duval
HEUBA BATOMEN Franck Duval - avatar
+ 1
No
19th Aug 2020, 10:54 AM
HEUBA BATOMEN Franck Duval
HEUBA BATOMEN Franck Duval - avatar
+ 1
M̷o̷u̷l̷i̷ 🇮🇳 Thanks For Suggestion! 😊
21st Aug 2020, 1:53 AM
Himanshu Rai
Himanshu Rai - avatar
+ 1
Null is abstract and non-zero (0==0)🤔
21st Aug 2020, 5:14 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Franck Pertinent don't understood. Please explain well
19th Aug 2020, 10:51 AM
Himanshu Rai
Himanshu Rai - avatar
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???
19th Aug 2020, 10:52 AM
Himanshu Rai
Himanshu Rai - avatar
0
Type is compare into object types String("12")!=12
19th Aug 2020, 10:55 AM
HEUBA BATOMEN Franck Duval
HEUBA BATOMEN Franck Duval - avatar
0
Franck Pertinent what you are saying, bad English🤔
19th Aug 2020, 10:57 AM
Himanshu Rai
Himanshu Rai - avatar
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???
19th Aug 2020, 11:04 AM
Himanshu Rai
Himanshu Rai - avatar
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!😊
21st Aug 2020, 1:52 AM
Himanshu Rai
Himanshu Rai - avatar