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 |
注意:引号("")中的值表示字符串值。在红色的值是程序员可能不希望被转换为的值。