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
注意:引号("")中的值表示字符串值。在红色的值是程序员可能不希望被转换为的值。
更多的JavaScript类型转换相关文章。
posted @ 2019-09-25 09:24  web小哥  阅读(710)  评论(0编辑  收藏  举报