Why use if (!!err)?

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


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.


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