var a = new Boolean(false); console.log(a);//{} console.log(a == false)//true console.log(a === false)//false if(a){ console.log("new Boolean(false) is true")//new Boolean(false) is true }else{ console.log("new Boolean(false) is false") }
这里a转换为true,因为a是一个Boolean的object,而object如果不是null或者undefined,就会被转换为true
这里主要需要分清楚值类型(primitive)和对象类型(object)
下面是JavaScript类型转换
JavaScript type conversions
值 | 转换为: | |||
---|---|---|---|---|
String | Number | Boolean | Object | |
undefined |
"undefined" |
NaN |
false |
throws TypeError |
null |
"null" |
0 |
false |
throws TypeError |
true |
"true" |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
use join() method |
|
|
|
|
NaN |
true |