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 !!!?

7
  • 1
    ! converts a value to the inverse boolean (if it was truthy it becomes false,falsy it becomes true) Commented Nov 11, 2013 at 15:43
  • 1
    !val === !!!val for any value of val Commented Nov 11, 2013 at 15:59
  • @Simon: I have never seen ====. Assuming you mean strict comparison, how is a Boolean operation the opposite of comparison? Commented Nov 11, 2013 at 16:02
  • 2
    Is this question not not not a joke? Commented Nov 11, 2013 at 16:21
  • @GuilhermeSehn You should clarify that !value would suffice for a boolean conversion. Commented May 19, 2014 at 15:50

5 Answers 5

4

One ! would be enough :)

!1 // false
!0 // true
Sign up to request clarification or add additional context in comments.

4 Comments

No, not if something comes back as undefined. !! forces undefined to be converted to true.
@antonpug: You sure about that? jsfiddle.net/kkf7Y A single ! seems to work just fine, at least in my browser.
@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.
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.

1 Comment

This answer is perfect and very clear.
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.

Comments

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.

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.