When working with JavaScript, it is important to note that almost all expressions have a "truthiness" value. This means that if you use an expression in a statement that expects a boolean, it will be evaluated as a boolean equivalent. For example:
let a = 'foo'
if (a) {
console.log('a is truthy!');
}
// Output: 'a is truthy!'.
Some workplaces have a common practice of coercing an expression into an actual boolean by negating it twice in this situation:
let a = 'foo'
if (!!a) {
console.log('a is truthy!');
}
// Output: 'a is truthy!'.
The question arises: Is this double negation merely a matter of style? Does it serve solely to communicate to other developers that we are aware a
is not a boolean but intend to evaluate it as such anyway? Or are there situations where the boolean value produced by if (a)
differs from if (!!a)
?