JavaScript Type Conversions
The below table shows the result of converting a value to different primitive data types.
- The string values are quoted with double-quotes.
- The values in bold are those that most programmers do not expect.
Value | Value Data Type | Converted to String | Converted to Number | Converted to Boolean |
"" | string | "" | 0 | false |
"0" | string | "0" | 0 | true |
"1" | string | "1" | 1 | true |
"10" | string | "10" | 10 | true |
"ten" | string | "ten" | NaN | true |
0 | number | "0" | 0 | false |
1 | number | "1" | 1 | true |
NaN | number | "NaN" | NaN | false |
Infinity | number | "Infinity" | Infinity | true |
-Infinity | number | "-Infinity" | -Infinity | true |
false | boolean | "false" | 0 | false |
true | boolean | "true" | 1 | true |
[ ] | array | "" | 0 | true |
[ 10 ] | array | "10" | 10 | true |
[ 10, 20 ] | array | "10,20" | NaN | true |
[ "ten" ] | array | "ten" | NaN | true |
[ "ten", "twenty" ] | array | "ten,twenty" | NaN | true |
null | null | "null" | 0 | false |
undefined | undefined | "undefined" | NaN | false |
function(){ } | function | "function(){ }" | NaN | true |
{ } | object | "[object Object]" | NaN | true |
Overall
We now know how a value can be converted to different primitive data types, like string, number, boolean, etc., in JavaScript.
Related Links
- JavaScript Tutorial
- JavaScript Examples