0

I recently came to know that null > -1 is true in javascript but did not find the answer why. So can someone please explain why?

enter image description here

Danish
  • 325
  • 1
  • 4
  • 12
  • 2
    because of the coercion to a number of `null` which is `0`. – Nina Scholz Mar 27 '20 at 09:39
  • https://tc39.es/ecma262/#sec-relational-operators – Teemu Mar 27 '20 at 09:40
  • What *should* it be? If you expect this to not be true, `null` would have to behave like `-Infinity` (smaller than any number) or `NaN` (any comparison is always false), which also wouldn't make a lot of sense. I'd accept an `IncompatibleTypeException` as alternative correct outcome, but Javascript doesn't throw exceptions from comparison operations by design… – deceze Mar 27 '20 at 09:42
  • @NinaScholz But ```null === 0``` is false ```null < 0``` is also false – Danish Mar 27 '20 at 09:45
  • `null` has not the same type of `0`. `null` is an object and `0` a number. – Nina Scholz Mar 27 '20 at 09:48

0 Answers0