Videos
Is null evaluated to 0 and undefined to NaN on arithmetic expressions? Is it safe or correct to assume this?
Yes, it is. An "arithmetic expression" would use the ToNumber operation:
Argument Type | Result
--------------+--------
Undefined | NaN
Null | +0
β¦ |
It is used in the following "arithmetic" expressions:
- prefix/postfix increment and decrement
- the unary
+and-operators - the
+operator if none of the two arguments is a string - subtraction, multiplication, division and modulo operation
- relational operators if not both arguments are strings
It is not used by the equality operators, so null == 0 is false (and null !== 0 anyway)!
It seems safe to assume so since, in an arithmetic expression (e.g. addition), the method ToNumber would be called on it, evaluating NaN and +0 from undefined and null respectively:
To Number Conversions
βββββββββββββββββ¦βββββββββββββββββββββββββββββββββββββββββββββ
β Argument Type β Result β
β ββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββ£
β Undefined β NaN β
β β β
β Null β +0 β
β β β
β Boolean β The result is 1 if the argument is true. β
β β The result is +0 if the argument is false. β
β β β
β Number β The result equals the input argument (no β
β β conversion). β
β β β
β String β See grammar and note below. β
β β β
β Object β Apply the following steps: β
β β 1. Let primValue be ToPrimitive(input β
β β argument, hint Number). β
β β 2. Return ToNumber(primValue). β
βββββββββββββββββ©βββββββββββββββββββββββββββββββββββββββββββββ
ECMAScript Language Specification - ECMA-262 Edition 5.1
To explain to a boss the difference between "zero" and "null":
"Zero" is a value. It is the unique, known quantity of zero, which is meaningful in arithmetic and other math.
"Null" is a non-value. It is a "placeholder" for a data value that is not known or not specified. It is only meaningful in this context; mathematical operations cannot be performed on null (the result of any such operation is undefined, and therefore also generally represented as null).
For example, as in the comments: "What is your yearly income?" is a question requiring a numeric answer. "0" is a perfectly valid answer for someone who does not work and has no investment income. If the user does not enter a value at all, they don't necessarily make no money; they just didn't want to tell your software how much (or little) they make. It's an unknown, not specified; therefore, to allow the software to continue, you specify the "null" placeholder for that data field within the software. That's technically valid from a data perspective; whether it's valid at the business level depends on whether an actual numeric value (even zero) is required in order to perform a mathematical operation (such as calculation of taxes, or comparison with thresholds determining benefits).
In computers, virtually any operation on a variable containing null will result either in null or in an error condition, because since one of the variable's values is not known, the result of the expression cannot be known. The equivalent of performing math on null would be if I asked you "What's five plus the number I'm thinking of right now?". It's impossible for you to give a definite answer because you don't know the number I'm thinking of. An operation on zero, except for dividing by it, is usually valid and will return another known, unique value.
Boss-speak is always tough...
Zero is a number so you can do things with it.
Null is a unicorn. It doesn't exist so you can't do anything at all with it.