Is !! a best practice to check a truthy value in an if statement

!!value is commonly used as a way to coerce value to be either true or false, depending on whether it is truthy or falsey, respectively.

In a control flow statement such as if (value) { ... } or while (value) { ... }, prefixing value with !! has no effect, because the control flow statement is already, by definition, coercing the value to be either true or false. The same goes for the condition in a ternary operator expression value ? a : b.

Using !!value to coerce value to true or false is idiomatic, but should of course only be done when it isn’t made redundant by the accompanying language construct.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)