布尔与Boolean

布尔与Boolean

取值

true和false

常用结构

if语句
while语句
for语句

转换方法

使用Boolean( )转换
隐式转换

隐式转化代码:
   let x = 50;
 let y = null;
 let z;
 if (x) {
    
    
   console.log(x);
 }
 if (y != null) {
    
    
   console.log(y);
 }
 if (z) {
    
    
   console.log(z);
 }
 //50

使用Boolean(value)方法可以强制转换任意值为boolean类型,除了以下六个值,其他都是自动转为true:

1、undefined

Boolean(undefined) // false

2、null

Boolean(null) // false

3、-0

Boolean(-0) // false

4、+0

Boolean(+0) // false

5、NaN

Boolean(NaN) // false

6、‘’(空字符串)


Boolean('') // false

猜你喜欢

转载自blog.csdn.net/outaidered/article/details/115865399