What is the !! (not not) operator in JavaScript?
What is the !! (not not) operator in JavaScript?
解答1
Coerces强制 oObject
to boolean. If it was falsey (e.g. 0, null
, undefined
, 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
What is the !! (not not) operator in JavaScript?相关推荐
- 核心(Core) Javascript 学习手记
说明: 一字一句都是俺敲出来的, ^^. 转载请注明出处哦. 前些日子, 发现好些地方需要用到 Js 代码, 以前没怎么系统的学习过, 要用到的时候查一堆一堆的手册, 写得不好不说, 还很浪费时间, ...
- JavaScript中的instanceof运算符是什么?
本文翻译自:What is the instanceof operator in JavaScript? The instanceof keyword in JavaScript can be qui ...
- 三个值得期待的JavaScript新功能!
让我们来看看JavaScript中一些有用的即将推出的功能.您将看到他们的语法,链接以及时了解他们的进度,我们将编写一个小型测试套件,以展示如何立即开始使用这些提案! JavaScript是如何更新迭 ...
- javascript 的 ~ 操作符作用
2019独角兽企业重金招聘Python工程师标准>>> "~" operator in javascript. For anyone else who also ...
- sql语句中的in用法示例_示例中JavaScript in操作符
sql语句中的in用法示例 One of the first topics you'll come across when learning JavaScript (or any other prog ...
- JavaScript 资源列表
这段时间闲着没事,就在JavaScript上多花点心思吧. 一.书籍 1. <JavaScript高级程序设计> 非常详尽而实用. 2. <JavaScript: The D ...
- (转载)JavaScript:双波浪号“~~“ 与 Math.floor()
[ 问题描述 ] 在网上看到了两种设置随机颜色的取值代码.但不知道"~~"是干什么用的? var a = (~~(Math.random() * 255)); //"~~ ...
- javascript运算符_双重否定运算符是什么! 用JavaScript做?
javascript运算符 Suppose you have an expression, which gives you a result. 假设您有一个表达式,它给您一个结果. You want ...
- JavaScript三元运算符
The ternary operator is the only operator in JavaScript that works with 3 operands, and it's a short ...
- Free Code Camp 练习总结 —— JavaScript 基本知识
我会每天都更新我写东西,学大概有一段时间了,我感触还是挺大的,把之前基础知识又复习一遍,活到老学到老.希望能给在FreecodeCamp有同学有帮助,我会每天更新我在写FreecodeCamp,代码. ...
最新文章
- 英语教授一针见血:背熟这500个固定搭配,英语成绩随便上100
- 字节跳动 1/3 员工不支持取消大小周,员工:每年少赚 10 万块
- 微娱推客——青龙羊毛
- 破天荒第一遭 安全公司因玩忽职守被客户告上法庭
- 用C++面向对象的方式动态加载so
- mysql分表方法实现
- linux ns机制,Linux内核API ns_to_timespec
- (50)FPGA面试题消除一个glitch实现
- linux bind mysql_linux下安装mysql
- Python菜鸟入门:day11文件操作
- Screens的开发一
- 数字电子技术基础第三版杨志忠_阎石《数字电子技术基础》(第6版)笔记和课后习题(含考研真题)详解复习笔记资料...
- 【xv6 RISC-V】xv6操作系统原理解析与源代码阅读报告
- 报错java.lang.NoClassDefFoundError: org/jaxen/JaxenException
- FPGA知识汇集-值得收藏的FPGA代码命名规范?
- [RF] 安装好Robot Framework之后怎样让启动的界面后面不带命令行窗口,且图片以机器人显示...
- linux下wifi连接方法
- 第三章 区块链进阶 [18]
- 门控时钟:Clock fating
- Qt::Painter 详解
热门文章
- html盒子模型的原理,新手对于CSS盒模型的认知
- cactiez mysql_cactiez v11添加对mysql数据库、apache系统进行监控
- kingroot android 7,KingRoot全球实现Android 7.0一键 Root
- 2.11.PHP7.1 狐教程-【PHP 数组】
- LayaAir UI 组件 # HSlider,VSlider(水平/垂直滑动条)
- 阶段3 1.Mybatis_09.Mybatis的多表操作_2 完成account表的建立及实现单表查询
- MAVEN项目标准目录结构(转)
- getResource()和getResourceAsStream以及路径问题
- SQL处理非常见空串
- Vue脚手架组件开发常见问题