0

Why is null > 3 false and null < 3 true in node?

$ node
> null > 3
false
> null < 3
true
FutoRicky
  • 903
  • 2
  • 9
  • 22

2 Answers2

6

Because it coerces null to 0 when applying the less/greater than operators.


Section 11.8.5 of the spec shows that the < and > operator will call ToNumber on the left value (null).

Section 9.3 of the spec shows that ToNumber will translate null to 0.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
0

I'm not 100% sure but it seems like null is 0 when using comparators but oddly null == 0 and null === 0 returns false but null <= 0 returns true when null < 0 returns false.

> null == 0 
false
> null === 0
false
> null <= 0
true
> null < 0
false
> null < -1
false