博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
What is the !! (not not) operator in JavaScript?
阅读量:5172 次
发布时间:2019-06-13

本文共 1091 字,大约阅读时间需要 3 分钟。

解答1

Coerces强制 oObject to boolean. If it was falsey (e.g. 0, nullundefined, etc.), it will be false, otherwise, true.

!oObject  //Inverted boolean!!oObject //Non inverted boolean so true boolean representation

So !! is not an operator, it's just the ! operator twice.

Real World Example "Test IE version":

let isIE8 = false; isIE8 = !! navigator.userAgent.match(/MSIE 8.0/); console.log(isIE8); // returns true or false

If you ⇒

console.log(navigator.userAgent.match(/MSIE 8.0/)); // returns either an Array or null

but if you ⇒

console.log(!!navigator.userAgent.match(/MSIE 8.0/)); // returns either true or false

It converts a nonboolean to an inverted boolean (for instance, !5 would be false, since 5 is a non-false value in JS), then boolean-inverts that so you get the original value as a boolean (so !!5 would be true).

 

解答2

! is "boolean not", which essentially typecasts the value of "enable" to its boolean opposite.

The second ! flips this value.

So, !!enable means "not not enable," giving you the value of enable as a boolean.

 

转载于:https://www.cnblogs.com/chucklu/p/11133975.html

你可能感兴趣的文章
lua 1.0 源码分析 -- 1 lua 的虚拟指令
查看>>
protoc-c 阅读笔记
查看>>
lua 1.0 源码分析 -- 2 内存回收
查看>>
lua 1.0 源码分析 -- 总结
查看>>
lua 源码阅读 1.1 -> 2.1
查看>>
mac被锁有pin码的解锁方法
查看>>
PyCharm黄色波浪线提示: Simplify chained comparison
查看>>
vue+elementui项目打包后样式变化问题
查看>>
mysql中的正则操作 匹配手机号,匹配中文,替换
查看>>
VSCode设置网页代码实时预览
查看>>
ionic3-修改APP应用图标(icon)和APP启动界面(Splash)
查看>>
jquery ui draggable,droppable 学习总结
查看>>
根据登陆用户获取相应权限菜单
查看>>
构建树形菜单数据
查看>>
Spring cloud服务调用(Feign)
查看>>
获取常用收藏菜单
查看>>
idea注册码至2100年
查看>>
获取到一个list 怎么分页?
查看>>
面试中的一天我是怎么度过的(含面试题)
查看>>
springboot2.1.1集成webService
查看>>