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

Leave a Comment