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. 10 Python之文件操作

    1.文件操作 f = open(文件路径, mode="模式", encoding="编码") f: 文件句柄 文件的路径: 相对路径 相对于当前程序所在的文件 ...

  2. resulting in duplicate entry '1' for key 'primary'

    现在有一个标签表,里面已经填入了一些数据了,想把主键生成策略改成自增的: ALTER TABLE `tags` CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUT ...

  3. AngularJs 初级入门 学习笔记

    刚学angular, 做一些笔记方便自己翻看. ng-app: 填写模块的名称 ng-init: 初始化数据(一般通过控制器初始化) ng-model: 填写数据模型 ng-bind: 绑定数据模型, ...

  4. 修改mysql的binlog的位置

    最近项目上装的mysql服务,分配的磁盘空间太小了,导致binlog两天时间就能打满,这里记录下处理方式 mysql的binlog日志是一个很重要的日志,以事件形式记录了所有的DDL和DML(除了数据 ...

  5. War of the Corporations CodeForces - 625B (greed)

    A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue the ...

  6. k8s master节点添加kubectl的使用

  7. 【转载】关于java 的InputStream和OutputStream的理解

    关于InputStream和OutputStream的输入输出方向的理解 InputStream输入类,首先需要读取的内容转化成输入流,再从它那里进行读取,先关联源:之后过程中关联目的,这样形成了流: ...

  8. 【BZOJ2752】【Luogu P2221】 [HAOI2012]高速公路

    不是很难的一个题目.正确思路是统计每一条边被经过的次数,但我最初由于习惯直接先上了一个前缀和再推的式子,导致极其麻烦难以写对而且会爆\(longlong\). 推导过程请看这里. #include & ...

  9. 【HDU6635】Nonsense Time

    题目大意:给定一个长度为 N 的序列,开始时所有的位置都不可用,每经过一个时间单位,都会有一个位置被激活.现给定激活顺序的序列,求每次激活之后全局的最长上升子序列的长度,保证数据随机. 题解: 引理: ...

  10. PMBOK :美国的项目管理知识体系

    PMBOK 是Project Management Body Of Knowledge的缩写, 指项目管理知识体系的意思,具体是美国项目管理协会(PMI)对项目管理所需的知识.技能和工具进行的概括性描 ...