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. JavaScript金字塔打印

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. C# 递归式快速排序算法

    static void Main(string[] args) { Console.WriteLine("************快速排序*****************"); ...

  3. Delphi 无类型文件

  4. 18、nginx优化

    一.性能优化概述 基询imm能优化,那么在性能优化这一章,我们将分为如下几个方面做介绍 1.首先我们需要了解性能优化要考虑哪些方面. 2.然后我们需要了解性能优化必须要用到的压力测试工具ab. 3.最 ...

  5. 504错误解决办法 让你的浏览器强制在后端服务器执行而不用通过前端CDN服务器

      因为后端执行时间过长,前端不等待,导致提示504错误的解决办法 504 错误是因为你的CDN服务器设置的延时有限, 超时导致的504 是前端不等待中止,是前端不行,后端应该正常 502 错误是后端 ...

  6. 解决 Jenkins 乱码以及命令不存在的问题

    方法一: Jenkins----系统管理----系统设置----全局属性----勾选环境变量 键 LANG 值 zh_CN.UTF-8 方法二(如果脚本用的是python): PYTHONIOENCO ...

  7. CSS中cursor属性

    光标类型   CSS十字准心 cursor: crosshair;手 cursor: pointer;cursor: hand;写两个是为了照顾IE5,它只认hand.等待/沙漏 cursor: wa ...

  8. python出现Non-ASCII character '\xe6' in file statistics.py on line 19, but no encoding declared错误

    可按照错误建议网址查看http://www.python.org/peps/pep-0263.html 发现是因为Python在默认状态下不支持源文件中的编码所致.解决方案有如下三种: 一.在文件头部 ...

  9. idea:spring initializr无web勾选,maven方式搭建springboot项目。jdk7创建springboot项目的版本不兼容问题。

    一.idea 使用spring initializr不选择web搭建springboot项目 1.file => new => project 2.直接next到finish结束. 3.完 ...

  10. 第二章 Vue快速入门-- 27 字符串的padStart方法使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...