Why null==0 equals to false??? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Why null==0 equals to false???

19th Aug 2020, 10:39 AM
Himanshu Rai
Himanshu Rai - avatar
12 Respostas
+ 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