您的位置:首页 > Web前端 > JavaScript

What is the !! (not not) operator in JavaScript?

2014-08-22 00:28 381 查看
http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript

Question:

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;

Answer:

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.
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).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: