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.
What is the !! (not not) operator in JavaScript?的更多相关文章
- 【转】The && and || Operator in JavaScript
原文: https://blog.mariusschulz.com/2016/05/25/the-andand-and-operator-in-javascript The && an ...
- The tilde ( ~ ) operator in JavaScript
From the JavaScript Reference on MDC, ~ (Bitwise NOT) Performs the NOT operator on each bit. NOT a y ...
- [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 ...
- JavaScript简易教程(转)
原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...
- JavaScript constructors, prototypes, and the `new` keyword
Are you baffled(阻碍:使迷惑) by the new operator in JavaScript? Wonder what the difference between a func ...
- JavaScript简易教程
这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScript的世界——前提是你有一些编程经验的话.本文试图描述这门语言的最小子集.我给这个子集起名叫做“Java ...
- 【转】Expressions versus statements in JavaScript
原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...
- javascript prototype原型链的原理
javascript prototype原型链的原理 说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: <script type="text/javasc ...
- JavaScript技巧手冊
js小技巧 每一项都是js中的小技巧,但十分的有用! 1.document.write(""); 输出语句 2.JS中的凝视为// 3.传统的HTML文档顺序是:documen ...
随机推荐
- LintCode 6---合并排序数组 II
import java.util.Arrays; public class Lint6 { /* * 合并两个排序的整数数组A和B变成一个新的数组.新数组也要有序. */ public static ...
- html2canvas+Canvas2Image分享海报功能踩坑
首先需要 import html2canvas from 'html2canvas'; import {Canvas2Image} from '../../assets/js/plug/canvas2 ...
- Linux查看修改文件句柄数
SuSE 11SP3 默认句柄数是1024 1.查看linux的文件句柄数 ulimit -a 2.修改文件句柄数 ①ulimit -n 65535②修改linux系统参数.vi /etc/secur ...
- 多线程编程-- part5.1 互斥锁之公平锁-释放锁
释放公平锁 1.unlock() unlock()在ReentrantLock.java中实现的,源码如下: public void unlock() { sync.release(1); } 说明: ...
- docker 网络 实现
最近在学习docker网络相关的知识,关于网络这块儿记下来,以便review dokcer安装完成之后默认提供三种网络 bridge host none docker默认使用bridge brid ...
- EfficientNet学习笔记
EfficientNet是谷歌大脑在2019年提出的,论文地址是:https://arxiv.org/pdf/1905.11946.pdf 这篇文章主要想解决的一个问题是,如何平衡网络的深度.宽度和分 ...
- puppet使用rsync模块同步目录和文件
puppet使用rsync模块同步目录和文件 2013-09-23 14:28:57 分类: LINUX 环境说明: OS : CentOS5.4 ...
- 1、检查并修改mysql的my.ini的配置文件
代码如下: default-character-set=utf8 2.建立数据库是要指定字符集 代码如下: create database mydb default character set utf ...
- Git账号Window10系统配置密钥
Git 拉取 推送 报错 . Window10系统 需要配置Git账号密钥.
- 【leetcode】1258. Synonymous Sentences
题目如下: Given a list of pairs of equivalent words synonyms and a sentence text, Return all possible sy ...