是什么 !! (不是)JavaScript中的运算符? [英] What is the !! (not not) operator in JavaScript?
本文介绍了是什么 !! (不是)JavaScript中的运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到一些代码似乎使用了一个我无法识别的运算符,以两个感叹号的形式出现,如下所示: !!
。有人可以告诉我这个运营商做了什么吗?
I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so: !!
. Can someone please tell me what this operator does?
我看到这个的背景是,
this.vertical = vertical !== undefined ? !!vertical : this.vertical;
推荐答案
强制 oObject
到布尔值。如果它是假的(例如0, null
, undefined
等),它将是 false
,否则, true
。
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.
真实世界示例测试IE版本:
Real World Example "Test IE version":
let isIE8 = false;
isIE8 = !! navigator.userAgent.match(/MSIE 8.0/);
console.log(isIE8); // returns true or false
如果你⇒
console.log(navigator.userAgent.match(/MSIE 8.0/));
// returns null
但是如果你⇒
console.log(!!navigator.userAgent.match(/MSIE 8.0/));
// returns true or false
这篇关于是什么 !! (不是)JavaScript中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文