The three most widely used type conversions are to string, to
number, and to boolean.
String Conversion – Occurs when we output something. Can be performed with
String(value). The conversion to string is usually obvious for primitive values.
Numeric Conversion – Occurs in math operations. Can be performed with Number(value).
The conversion follows the rules:
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.
Boolean Conversion – Occurs in logical operations. Can be performed with
Boolean(value).
Follows the rules:
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.