-2

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 ?

Pushpendra Kumar
  • 1,721
  • 1
  • 15
  • 21
  • Why *wouldn't* it be happening? What is it specifically that surprises you about these results? Have you looked into what `null` is and how `>` and `==` work? – T.J. Crowder Jun 16 '22 at 08:34
  • you can refer this. [enter link description here](https://stackoverflow.com/questions/2910495/why-null-0-null-0-but-not-null-0) – user19205285 Jun 16 '22 at 08:45

2 Answers2

0

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) 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.

Comparisons

MinD
  • 109
  • 6
  • Please post code, error messages, markup, and other textual information **as text**, not just as a *picture* of text. Why: http://meta.stackoverflow.com/q/285551/157247 – T.J. Crowder Jun 16 '22 at 08:37
0

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
ardritkrasniqi
  • 739
  • 5
  • 13