views:

1107

answers:

5

I've seen 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?

+5  A: 

I think that would just be !(!a), yes?

That is a logical NOT against another logical NOT.

As in:

true = !false == !!true
GMan
+14  A: 

Converts it to boolean!

!oObject  //Inverted boolean
!!oObject //Non inverted boolean so true boolean representation
Stevo3000
This makes perfect sense...
Hexagon Theory
To elaborate, it converts a non-boolean to a boolean, then inverts it. I'm not so hot on javascript, but that sounds like casting to a boolean to me....
Darren Clark
@Darren Clark - Wrong way round.
Stevo3000
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).
Chuck
An easy way to describe it is: Boolean(5) === !!5;Same casting, fewer characters.
dfltr
Also, !! is not an operator. It's just the ! operator twice.
August Lilleaas
+6  A: 

It's just the logical NOT operator, twice - it's used to convert something to boolean, e.g.:

true === !!10

false === !!0
Greg
Why on earth would you do that? Use the ! operator to convert to boolean then use === to compare type? Just accept you have no type safety and do val > 0 or something.
Darren Clark
+1  A: 

It seems that the !! operator results in a double negation.

var foo = "Hello World!";

!foo // Result: false
!!foo // Result: true

Steve

Steve Harrison
+1  A: 

I suspect this is a leftover from C++ where people override the ! operator but not the bool operator.

So to get a negative(or positive) answer in that case you would first need to use the ! operator to get a boolean, but if you wanted to check the positive case would use !!.

Darren Clark