转换成字符串
null 、undefined 没有 toString() 方法
[].toString() [1, 2].toString() ({}).toString()
[1, '123', [], undefined, null, NaN, true ].toString()
|
转换成数值
Number('123') Number('123c')
|
parseInt('12.3')
parseInt('15xyz') parseInt('15x6yz')
|
var d = '345' +d === 345 d - 0 === 345 d * 1 === 345 d / 1 === 345
|
+[] +['2'] +['1', '2'] +{}
|
转换成布尔值
0, NaN, 空字符串,undefined, null, false 会被转换成为 false
Boolean(123)
Boolean('123') Boolean(' ') Boolean('')
Boolean(undefined) Boolean(null)
|
if ('') { console.log('哈哈') }
|
变量转换表
Value |
Boolean |
Number |
String |
undefined |
false |
NaN |
“undefined” |
null |
false |
0 |
“null” |
true |
true |
1 |
“true” |
false |
false |
0 |
“false” |
“” |
false |
0 |
“” |
“123” |
true |
123 |
“123” |
“1a” |
true |
NaN |
“1a” |
0 |
false |
0 |
“0” |
1 |
true |
1 |
“1” |
Infinity |
true |
Infinity |
“Infinity” |
NaN |
false |
NaN |
“NaN” |
{} |
true |
NaN |
“[object Object]” |
数组 |
true |
空数组为 0,存在一个元素且为数字转数字,其他 NaN |
[1, ‘123’, [], undefined, null, NaN, true ] => “1,123,,,,NaN,true” |