JS primitive type conversions
JS primitive type conversions
Value Becomes…
undefined NaN
null 0
true/false 1 / 0
The string is read “as is”, whitespaces from both sides are ignored. An empty string
string
becomes 0. An error gives NaN.
Value Becomes…
0, null, undefined, NaN, "" false
any other value true
Most of these rules are easy to understand and memorize. The notable exceptions where people usually
make mistakes are:
• undefined is NaN as a number, not 0.
• "0" and space-only strings like " " are true as a boolean.