can someone explain the null comparison with 0
null > 0 // false
null == 0 // false
null > 0 || null == 0 // false
null >= 0 // true
why it happening ?
CodePudding user response:
The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, treating it as 0. That’s why null >= 0 is true and 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 null == 0 is false.
Source: Comparisons
CodePudding user response:
In Javascript null isn't equal to 0 but equal to undefined.
null == undefined // true
Also :
Strange result: null vs 0
Let’s compare null with a zero:
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) trueMathematically, 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.
CodePudding user response:
or check this process. https://262.ecma-international.org/5.1/#sec-11.8.5