Why null==0 equals to false??? | Sololearn: Learn to code for FREE!

0

Why null==0 equals to false???

8/19/2020 10:39:57 AM

Himanshu Rai

12 Answers

New Answer

+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

+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!😊