JavaScript 类型转换
JavaScript 类型转换表
下表显示了将不同的 JavaScript 值转换为数字、字符串和布尔值的结果:
Original 值 |
Converted to Number |
Converted to String |
Converted to 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 | 试一试 » |
注释: 引号 ("") 中的值表示字符串值。 red 中的值表示(某些)程序员可能不期望的值。
有关 JavaScript 类型转换的教程,请阅读我们的 JavaScript 类型转换教程。