What is the !! (not not) operator in JavaScript?

解答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.

What is the !! (not not) operator in JavaScript?的更多相关文章

  1. 【转】The && and || Operator in JavaScript

    原文: https://blog.mariusschulz.com/2016/05/25/the-andand-and-operator-in-javascript The && an ...

  2. The tilde ( ~ ) operator in JavaScript

    From the JavaScript Reference on MDC, ~ (Bitwise NOT) Performs the NOT operator on each bit. NOT a y ...

  3. [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript

    Sometimes we might want to make a function more generic by having it accept a union of different typ ...

  4. JavaScript简易教程(转)

    原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...

  5. JavaScript constructors, prototypes, and the `new` keyword

    Are you baffled(阻碍:使迷惑) by the new operator in JavaScript? Wonder what the difference between a func ...

  6. JavaScript简易教程

    这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScript的世界——前提是你有一些编程经验的话.本文试图描述这门语言的最小子集.我给这个子集起名叫做“Java ...

  7. 【转】Expressions versus statements in JavaScript

    原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...

  8. javascript prototype原型链的原理

    javascript prototype原型链的原理 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: <script type="text/javasc ...

  9. JavaScript技巧手冊

    js小技巧 每一项都是js中的小技巧,但十分的有用! 1.document.write(""); 输出语句  2.JS中的凝视为//  3.传统的HTML文档顺序是:documen ...

随机推荐

  1. jenkins操作TreeView,展开合并

    双击treeview 双击选中的部分,使treeview展开合并 Opt() #include <GUIConstantsEx.au3> #include <GuiTreeView. ...

  2. slf4j日志的使用-学习笔记

    maven项目: 一.首先在pom.xml文件中添加maven依赖 这是其中一种: <dependency>     <groupId>org.slf4j</groupI ...

  3. python视频学习笔记4(函数)

    函数中return和print的区别,没有return会默认返回None值 函数定义:所谓**函数**,就是把 **具有独立功能的代码块** 组织为一个小模块,在需要的时候 **调用** 1.函数的步 ...

  4. Windows设置 .exe 开机自启动

    例如:想让Nginx开机自启动 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

  5. Python package钓鱼

    Python package钓鱼   一.概述 在收录该文之后,知道创宇404安全实验室对该文中所提到的攻击方式进行跟进.整理分析原作者公布的钓鱼数据.值得一提的是,在跟进的过程中,我们发现了新的钓鱼 ...

  6. emwin之BUTTON控件显示位图和流位图出现卡顿延迟的情况

    @2019-05-16 [问题] 参照Armfly的emwin教程第46章 BUTTON-按钮控件显示位图和流位图,实际使用时导致界面切换卡顿延迟较大的情况 [环境] F429IGT6 + W9825 ...

  7. VMware Tools按钮变灰色,无法安装的解决方法

    参考博客: https://blog.csdn.net/weixin_30639719/article/details/94846851 https://jingyan.baidu.com/artic ...

  8. inode,软硬链接

    如何查看inode ll -di /boot / /app查看文件和文件夹的inode号 df -i查看挂载点文件夹的inode号 做inode增长实验 创建60万个文件的方法1(效率不高):for ...

  9. R 语言中的简单线性回归

    ... sessionInfo() # 查询版本及系统和库等信息 getwd() path <- "E:/RSpace/R_in_Action" setwd(path) rm ...

  10. BZOJ2656 [Zjoi2012]数列(sequence)[模拟]

    这个递推式子可以发现$i$是偶数下标可以缩一半,是奇数下标就可以拆成两个下标,$\lfloor \frac{i}{2} \rfloor$以及$\lfloor \frac{i}{2}+1 \rfloor ...