4

In this code sample from the sequlize docs:

if (!!err) {
    console.log('Unable to connect to the database:', err)
} else {
    console.log('Connection has been established successfully.')
}

Why are they using (!!err) to test that err's truthiness? Isn't that the same as if (err)?

Salman A
  • 229,425
  • 77
  • 398
  • 489
eric
  • 2,459
  • 4
  • 24
  • 39
  • 2
    As far as I know it is the same... – brso05 Dec 02 '14 at 19:56
  • 2
    Yes, it's the same for checking truthiness. I imagine it's just people being pedantic about testing an actual boolean in the conditional expression over letting the interpreter coerce it for them. IMHO, unless you want to explicitly return a boolean (e.g. `return !!err;`), a `!!` is superfluous. – ajp15243 Dec 02 '14 at 20:01
  • 1
    Not quite the same ... err is an object. !! converts it to an inverted boolean, then inverts the boolean again ... If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true. So !! is not an operator, it's just the ! operator twice. – rfornal Dec 02 '14 at 20:02
  • 1
    It does not serve any purpose inside the `if` statement, anything truthy triggers the if block. – Salman A Dec 02 '14 at 20:04
  • 4
    This question is NOT a duplicate of http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript. That question provides info about the `!!` syntax and what it does, but does not explain why it is used in the OP's question. That is a further question and it turns out the answer is that there is no need for that syntax in the OP's code example since `if (err)` and `if (!!err)` generate the same logic either way. – jfriend00 Dec 02 '14 at 20:09
  • See also [Why use `!!` to coerce a variable to boolean for use in a conditional expression?](https://stackoverflow.com/q/18648179/1048572) and [When to use the double not (`!!`) operator in JavaScript](https://stackoverflow.com/q/2174297/1048572) – Bergi Aug 04 '17 at 06:51

3 Answers3

12

Why are they using (!!err) to test that err's truthiness?

There's no reason. Maybe they're overcautious, having heard some wrong things about thruthiness? Or they want to emphasize the ToBoolean cast that occurs in the evaluation of the if condition?

Isn't that the same as if (err)?

Yes.

if (err)
if (Boolean(err))
if (!! err)

all mean exactly the same thing. The latter two only doing unnecessary steps in between before arriving at the same result.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
2

Double exclamation mark convert any thing to the Boolean representation of that expression whether it is true or false in sense of Boolean value

OR

It converts a nonboolean to an inverted boolean

for example define a string

var vari = "something";

and check for its boolean equivalent

console.log(!!vari);

Why part of you Question

Author or writer may be over Precocious to check of the existence of the error. Or have an extra check that some one is not passing an expression instead of error that need to be checked as boolean. In Both ways it is doing same thing So i explained what it means you dont need to worry about why now as they are same authors intent can be

!! intent is usually to convey to the reader that the code does not care what value is in the variable, but what it's "truth" value is.

As Bhojendra - C-Link Nepal gave example

err = 'This is also to be a true if you convert it to boolean';
if(!!err == 1){
  console.log('test by converting to boolean');
}
if(err == 1){
  console.log('a real boolean test');
}

one is considered true but 'this sentence' is not equal to 1 , so being true still one if block will be executed in above code

A.B
  • 17,478
  • 3
  • 28
  • 54
  • 4
    I don't think this answers the question because won't `if (err)` generate the same branching as `if (!!err)`? So, we're still wondering "Why use if (!!err)?" – jfriend00 Dec 02 '14 at 19:59
  • 1
    @jfriend00 NO. This may be a false condition too. But if(err) is a true condition – Bhojendra Rauniyar Dec 02 '14 at 20:01
  • 2
    @Bhojendra-C-LinkNepal - for what value of `err` does `if (err)` generate a different result than `if (!!err)`. Only that will answer the question. – jfriend00 Dec 02 '14 at 20:01
  • 1
    I agree. It doesn't seem to be necessary in the context of an `if` statement where `if (null)` is still falsy. – Jeff Fairley Dec 02 '14 at 20:02
  • 3
    As Bergi's answer shows, this answer is not an answer to the question. It is information about `!!`, but is not an answer to the question. – jfriend00 Dec 02 '14 at 20:03
  • @jfriend00 have edited for your question about Why :) – A.B Dec 02 '14 at 20:07
  • @jfriend00 Due to hang of my pc, I'm late to explain what I wanted to say, please check my answer to verify what I wanted to comment above... – Bhojendra Rauniyar Dec 02 '14 at 20:16
1

It really matters the same thing.

But, wait! It's really different thing when we use to test true or false:

err = 'This is also to be a true if you convert it to boolean';
if(!!err == 1){
  console.log('test by converting to boolean');
}
if(err == 1){
  console.log('a real boolean test');
}

//outputs only: test by converting to boolean

But a real boolean value logs both as in the following example:

err = true;
if(!!err == 1){
  console.log('test by converting to boolean');
}
if(err == 1){
  console.log('a real boolean test');
}

So double exclamation sign is used to convert to a boolean only.

So, people use the condition to make sure the value is in boolean or not and if it's not then convert it and check it but if he wanted to check strictly that is true or not then he'll compare using 1 === 1 and in the above example exactly the same thing is being applied.

But just like in your code if(!!err){ is used to make sure it is false or true because of lack of deep knowledge that if(err){//string even can be checked as true or false.

That's it. The exactly check is made by !!.

Bhojendra Rauniyar
  • 73,156
  • 29
  • 131
  • 187
  • yes thats my point , great example @Bhojendra (Y), i wish you can add this thing to my answer also – A.B Dec 02 '14 at 20:17
  • You are sharing information about `!!`, but not answering the question about why it is used in the context of `if (!!err)`. This is not an answer to the question. People need to pay attention to the question that is asked. There's plenty of info about `!!` already here: http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript – jfriend00 Dec 02 '14 at 20:17
  • actually he has provide genuine reason wbout "why" part of your answer same was i doing , you can closely look at the example .you will get to know what he is trying to say – A.B Dec 02 '14 at 20:21
  • @A.B Please feel free to combine my answer. – Bhojendra Rauniyar Dec 02 '14 at 20:40
  • (Y) means i liked that you feel free to share your answer mate – A.B Dec 02 '14 at 20:45
  • exact meaning? 1. like 2. like and share feelings ??? which one? I'm really interested in english. – Bhojendra Rauniyar Dec 02 '14 at 20:52
  • first..1. Like equivalent to (Y) – A.B Dec 02 '14 at 21:02
  • 1
    What does that `== 1` thing have to do with the question? And why are you comparing a *boolean* value to the number 1 anyway? – Bergi Dec 03 '14 at 01:18
  • You may compare with true instead of 1, and the result will be the same. – Bhojendra Rauniyar Dec 03 '14 at 08:37