js中 ! 的用法是比较灵活的,它除了做逻辑运算常常会用!做类型判断,可以用!与上对象来求得一个布尔值,!可将变量转换成boolean类型,0,null、undefined和空字符串取反都为true,其余都为false。

JS中!!的用法

“!!” ——两个叹号表示把目标值转化为布尔值,相当于使用Boolean()方法

!!"123"  相当于是   Boolean("123")    //结果为true
!!{a:1}  相当于是    Boolean({a:1})    //结果为true

Boolean是不必要的。

在eslint验证中会报下列错误:

Redundant double negation.eslint(no-extra-boolean-cast)





相关文章:

  • 2021-05-17
  • 2021-09-01
  • 2021-10-23
  • 2022-02-07
  • 2022-02-07
  • 2021-05-20
  • 2021-08-08
猜你喜欢
  • 2021-11-25
  • 2021-12-03
  • 2021-12-04
  • 2021-12-04
  • 2021-12-04
  • 2021-12-19
  • 2021-07-25
相关资源
相似解决方案