Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; 
like image 922
Hexagon Theory Avatar asked Apr 24 '09 08:04

Hexagon Theory


People also ask

What is the NOT operator?

What Does NOT Operator Mean? In Boolean algebra, the NOT operator is a Boolean operator that returns TRUE or 1 when the operand is FALSE or 0, and returns FALSE or 0 when the operand is TRUE or 1. Essentially, the operator reverses the logical value associated with the expression on which it operates.

What does != Mean in JavaScript?

The inequality operator ( != ) checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types.

What does '!' Mean in JavaScript?

symbol is used to indicate whether the expression defined is false or not. For example, !( 5==4) would return true , since 5 is not equal to 4. The equivalent in English would be not .


1 Answers

Converts Object 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":

const 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 
like image 180
stevehipwell Avatar answered Sep 20 '22 18:09

stevehipwell