下表顯示了將不同的JavaScript值轉換為Number,String和Boolean的結果: 註意:引號("")中的值表示字元串值。在紅色的值是程式員可能不希望被轉換為的值。 更多的JavaScript類型轉換相關文章。 ...
下表顯示了將不同的JavaScript值轉換為Number,String和Boolean的結果:
原始值 | 轉換為Number | 轉換為String | 轉換為Boolean |
---|---|---|---|
false | 0 | "false" | false |
true | 1 | "true" | true |
0 | 0 | "0" | false |
1 | 1 | "1" | true |
"0" | 0 | "0" | true |
"1" | 1 | "1" | true |
NaN | NaN | "NaN" | false |
Infinity | Infinity | "Infinity" | true |
-Infinity | -Infinity | "-Infinity" | true |
"" | 0 | "" | false |
"20" | 20 | "20" | true |
"twenty" | NaN | "twenty" | true |
[ ] | 0 | "" | true |
[20] | 20 | "20" | true |
[10,20] | NaN | "10,20" | true |
["twenty"] | NaN | "twenty" | true |
["ten","twenty"] | NaN | "ten,twenty" | true |
function(){} | NaN | "function(){}" | true |
{ } | NaN | "[object Object]" | true |
null | 0 | "null" | false |
undefined | NaN | "undefined" | false |