It isn't preferable, or even different, in this case. The double-bang converts the variable that follows it into a Boolean value (similar to wrapping in the Boolean()
constructor). Gets around potential problems with truthy and falsey variables.
However, putting the variable alone in an if()
clause does the same thing (it also resolves the contents of the if
's parens to a hard Boolean).
The double-bang can prove helpful in other cases, such as if you want to return a true
or false
at the end of a function based on a variable's truthiness/falsiness. That would be rather than:
if(condition) {
return true;
} else {
return false;
}
You could simply write:
return !!condition;