I had to write a routine that increments the value of a variable by 1 if its type is number and assigns 0 to the variable if not, where the variable is initially null or undefined.
The first implementation was v >= 0 ? v += 1 : v = 0 because I thought anything not a number would make an arithmetic expression false, but it was wrong since null >= 0 is evaluated to true. Then I learned null behaves like 0 and the following expressions are all evaluated to true.
null >= 0 && null <= 0!(null < 0 || null > 0)null + 1 === 11 / null === InfinityMath.pow(42, null) === 1
Of course, null is not 0. null == 0 is evaluated to false. This makes the seemingly tautological expression (v >= 0 && v <= 0) === (v == 0) false.
Why is null like 0, although it is not actually 0?
nullorundefined:c = -~c // Results in 1 for null/undefined; increments if already a number- Ates Goralundefinedis a variable value, for variables that have not been initialized.null, on the other hand, is an empty object value, and should not be mixed with numbers.nullshould not be combined with numbers, so null should not have to behave like numbers. - Matthew