Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript comparison question (null >= 0) [duplicate]

How should I understand these?

null>0 > false  null==0 > false  null>=0 > true 
like image 994
eonil Avatar asked Jul 30 '10 14:07

eonil


People also ask

Why is null >= 0 true?

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.

Why does JavaScript use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

Is null greater than 0 JS?

Example. In the following example, null is not greater than 0 and not equal to 0, but greater than or equal to 0. It looks very odd to hear. Because in mathematics if we have two numbers i.e a, b and if a is not less than the b then the possible scenarios are either a is greater than b or a is equal to b.

What is == and === in JavaScript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.


1 Answers

The relational operators (>= and <=), perform type coercion (ToPrimitive), with a hint type of Number, all the relational operators present have this behavior.

You can see the inner details of this process in the The Abstract Relational Comparison Algorithm.

On the other hand, the Equals operator (==), if an operand is null it only returns true if the other is either null or undefined, no numeric type coercion is made.

null == undefined; // true null == null; // true 

Check the inner details of this process in the The Abstract Relational Comparison Algorithm.

Recommended articles:

  • typeof, == and ===
  • Notes on ECMAScript Equality Operators
like image 198
Christian C. Salvadó Avatar answered Sep 22 '22 11:09

Christian C. Salvadó