# 1. 其它数据类型转布尔值
转化为布尔值的情况是很简单的。
当我们在使用Boolean()来进行转换时,有如下转换规则:
| 参数类型 | 结果 |
|---|---|
| false、undefined、null、+0、-0、NaN、“” | false |
| 除了上面的情况 | true |
(另外需要注意的是,如果在使用Boolean()时不传参数结果也是为false的)
# 1.1 数字转布尔值
数字转布尔值,只需要记住:
- 除了
0, -0, NaN这三种转换为false,其他的一律为true。
# 1.1.1 题目一
console.log(Boolean(0))
console.log(Boolean(-0))
console.log(Boolean(NaN))
console.log(Boolean(1))
console.log(Boolean(Infinity))
console.log(Boolean(-Infinity))
console.log(Boolean(100n))
console.log(Boolean(BigInt(100)))
@前端进阶之旅: 代码已经复制到剪贴板
记住上面👆的规律,这边我把bigInt类型的也拿过来试了一下,发现它也是为true。
因此答案为:
console.log(Boolean(0)) // false
console.log(Boolean(-0)) // false
console.log(Boolean(NaN)) // false
console.log(Boolean(1)) // true
console.log(Boolean(Infinity)) // true
console.log(Boolean(-Infinity)) // true
console.log(Boolean(100n)) // true
console.log(Boolean(BigInt(100))) 