Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

javascript - 是什么 !! (不是)JavaScript中的运算符?(What is the !! (not not) operator in 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?

(有人可以告诉我这个操作员做什么吗?)

The context in which I saw this was,

(我看到的背景是)

this.vertical = vertical !== undefined ? !!vertical : this.vertical;
  ask by Hexagon Theory translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Coerces oObject to boolean.

(将oObject oObject为布尔值。)

If it was falsey (eg 0, null , undefined , etc.), it will be false , otherwise, true .

(如果它为假(例如0, nullundefined等),则为false ,否则为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":

(真实示例“测试IE版本”:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...