1

!! operator is obviously useful as it forces to check something that's not a boolean as a Boolean - that's for "True" conditions.

But what about false? Do you need to use !!!?

antonpug
  • 13,724
  • 28
  • 88
  • 129
  • 1
    ! converts a value to the inverse boolean (if it was truthy it becomes false,falsy it becomes true) – megawac Nov 11 '13 at 15:43
  • 1
    `!val === !!!val` for any value of val – Tibos Nov 11 '13 at 15:59
  • @Simon: I have never seen `====`. Assuming you mean strict comparison, how is a Boolean operation the opposite of comparison? – Felix Kling Nov 11 '13 at 16:02
  • 2
    Is this question not not not a joke? – Emissary Nov 11 '13 at 16:21
  • @GuilhermeSehn You should clarify that !value would suffice for a boolean conversion. – geoyws May 19 '14 at 15:50
  • @Emissary People do come by !!! in other people's code and they need reassurance for their sanity. For example, Node.js (downloaded 5 days ago) uses !!!value in their assert.js. [Github shows that they've changed it in July 2013 though.](https://github.com/joyent/node/commit/dc9acd4faeba1aade414bdd8da28f30b16773575) Why does my version of Node not reflect this? – geoyws May 19 '14 at 15:50
  • There is no `(!!!)` operator in JavaScript. `(!!!) true` → `Exception: expected expression, got ')'` – Nicolas Barbulesco May 12 '15 at 18:19

5 Answers5

4

One ! would be enough :)

!1 // false
!0 // true
andbas
  • 591
  • 4
  • 8
  • No, not if something comes back as undefined. !! forces undefined to be converted to true. – antonpug Nov 11 '13 at 15:47
  • 1
    @antonpug: You sure about that? http://jsfiddle.net/kkf7Y/ A single `!` seems to work just fine, at least in my browser. – David Nov 11 '13 at 15:49
  • http://stackoverflow.com/questions/4686583/can-someone-explain-this-double-negative-trick – antonpug Nov 11 '13 at 15:50
  • 1
    @antonpug ok, the link you posted only show that I'm right. !! will never return true on undefined. Single ! convert any value to Boolean and apply the NOT operation to it, second ! will just apply one more NOT. – andbas Nov 11 '13 at 21:55
3

!! is not an operator in itself; it is merely a way of using JavaScript's logical NOT (!) operator.

To convert x to a boolean, !!x works because when you negate a boolean twice, you get the original boolean, and !x converts x to a boolean before negating it.

Likewise, !!!x converts x to a boolean and negates it three times, which is equivalent to negating it only once. So you can use !x instead of !!!x.

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
1

that's for "True" conditions.

No, it's for any value. !! will convert any value in it's equivalent Boolean value.

Why? Because the not operator simply returns the opposite Boolean value. So

0    // is falsy
!0   // -> true
!!0  // -> false

'foo'    // is truthy
!'foo'   // -> false
!!'foo'  // -> true

Any additional application will just toggle the value again. Thus !!! is equivalent to ! and !!!! is equivalent to !! and so on.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0
var boolCheck = 0;
   boolCheck //0
  !boolCheck //true
 !!boolCheck //false
!!!boolCheck //true - again

Same thing here:

var boolCheck = 1;
   boolCheck //1
  !boolCheck //false
 !!boolCheck //true
!!!boolCheck //false - again

There is no reason to use this ( !!! ), it is a tautology. !! is useful when:

  1. we need to have a Boolean value, when there are some results ( !!5 => true );
  2. we need to shorten false with !1 or true - !0.
    1. or to skip the wrapper.
Bakudan
  • 19,134
  • 9
  • 53
  • 73
-1

This answers your question perfectly https://stackoverflow.com/a/264037/1561922

!!!x is probably inversing a boolean conversion !!x:

var myBool = Boolean("false");  // == true

var myBool = !!"false";  // == true

"Any string which isn't empty will evaluate to true"

So !!!"false"; // == false

This question is NOT a joke. Node.js (downloaded 5 days ago) uses this in Assert.js for example:

function ok(value, message) {
  if (!!!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;

EDIT: I think they did it for code readability reasons out of habit, because !value already suffices.

EDIT: Node changed it. I have no idea why my version of Node.js that was downloaded 5 days ago is still with the !!!value instead of the !value in GitHub.

EDIT: This is the reason why.

Community
  • 1
  • 1
geoyws
  • 3,326
  • 3
  • 35
  • 47