Variable coercion / conversion:
*Primitive to primitive conversion table
| Value | Converted To String | Converted To Number | Converted To Boolean |
|---|---|---|---|
| undefinded | “undefined” | NaN | No |
| null | “null” | 0 | No |
| true | “true” | 1 | No |
| false | “false” | 0 | No |
| NaN | “NaN” | No | |
| “” empty string | 0 | No | |
| ” “ | 0 | Yes | |
| “2.4” (numeric) | 2.4 | Yes | |
| “test” (non numeric | NaN | Yes | |
| “0” | 0 | Yes | |
| “1” | 1 | Yes | |
| -0 | “0” | No | |
| 0 | “0” | No | |
| 1 | “1” | Yes | |
| Infinity | “Infinity” | Yes | |
| -Infinity | “-Infinity” | Yes | |
| [] | “” | 0 | Yes |
| [3] | “3” | 3 | Yes |
| [‘a’] | “a” | NaN | Yes |
| [‘a’,’b’] | “a,b” | NaN | Yes |
| { } | “[object Object]” | NaN | Yes |
| function(){} | “function(){}” | NaN | Yes |
Bold values highlight conversion that programmers may find surprising
To convert explicitly values you can use String() Number() Boolean()