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
2009-04-24 08:17:28
+14
A:
Converts it to boolean!
!oObject //Inverted boolean
!!oObject //Non inverted boolean so true boolean representation
Stevo3000
2009-04-24 08:18:07
This makes perfect sense...
Hexagon Theory
2009-04-24 08:20:16
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
2009-04-24 08:36:56
@Darren Clark - Wrong way round.
Stevo3000
2009-04-24 09:03:25
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
2009-04-24 17:14:18
An easy way to describe it is: Boolean(5) === !!5;Same casting, fewer characters.
dfltr
2009-04-24 18:27:27
Also, !! is not an operator. It's just the ! operator twice.
August Lilleaas
2010-04-07 02:19:52
+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
2009-04-24 08:18:49
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
2009-04-24 08:41:48
+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
2009-04-24 08:20:19
+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
2009-04-24 08:33:39