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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With