Boolean判断

98 阅读1分钟

知识点

  1. !!:就是强制把一个变量的数据类型转成布尔值;
  2. 转换成布尔类型为 false 的值有:0、-0、''、undefined、false、NaN、null;
  3. 空数组、空对象转成布尔值时是 true,因为对象是引用类型,里面存储的是引用地址。
  4. NaN与任何数据做符号运算结果都为false
数据类型转换为true的值转换为false的值
Booleantruefalse
String任何非空字符串空字符串
Number任何非零的数值(包括无穷大)0和NaN
Object任何对象null
Undefinedundefined

注意点

//null 和 undefined 不能被隐式转换。
console.log(null==0)//false
console.log(null==undefined)//false