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
原始值 转换为Number 转换为String 转换为Boolean 原始值 转换为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 false 0 “false” false false

false

0

0

“false”

“false”

false

false

true 1 “true” true true

true

1

1

“true”

“true”

true

true

0 0 “0” false 0

0

0

0

“0”

“0”

false

false

1 1 “1” true 1

1

1

1

“1”

“1”

true

true

“0” 0 “0” true “0”

“0”

0

0

“0”

“0”

true

true

“1” 1 “1” true “1”

“1”

1

1

“1”

“1”

true

true

NaN NaN “NaN” false NaN

NaN

NaN

NaN

“NaN”

“NaN”

false

false

Infinity Infinity “Infinity” true Infinity

Infinity

Infinity

Infinity

“Infinity”

“Infinity”

true

true

-Infinity -Infinity “-Infinity” true -Infinity

-Infinity

-Infinity

-Infinity

“-Infinity”

“-Infinity”

true

true

“” 0 “” false “”

“”

0

0

“”

“”

false

false

“20” 20 “20” true “20”

“20”

20

20

“20”

“20”

true

true

“twenty” NaN “twenty” true “twenty”

“twenty”

NaN

NaN

“twenty”

“twenty”

true

true

[ ] 0 “” true [ ]

[ ]

0

0

“”

“”

true

true

[20] 20 “20” true [20]

[20]

20

20

“20”

“20”

true

true

[10,20] NaN “10,20” true [10,20]

[10,20]

NaN

NaN

“10,20”

“10,20”

true

true

[“twenty”] NaN “twenty” true [“twenty”]

[“twenty”]

NaN

NaN

“twenty”

“twenty”

true

true

[“ten”,”twenty”] NaN “ten,twenty” true [“ten”,”twenty”]

[“ten”,”twenty”]

NaN

NaN

“ten,twenty”

“ten,twenty”

true

true

function(){} NaN “function(){}” true function(){}

function(){}

NaN

NaN

“function(){}”

“function(){}”

true

true

{ } NaN “[object Object]” true { }

{ }

NaN

NaN

“[object Object]”

“[object Object]”

true

true

null 0 “null” false null

null

0

0

“null”

“null”

false

false

undefined NaN “undefined” false undefined

undefined

NaN

NaN

“undefined”

“undefined”

false

false

注意:引号(””)中的值表示字符串值。在红色的值是程序员可能不希望被转换为的值。
更多的JavaScript类型转换相关文章。

JavaScript类型转换

» 本文来自:前端开发者 » 《JavaScript 数据类型转换表_梵醉_前端开发者》
» 本文链接地址:https://www.rokub.com/73372.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!