2

Is there any reason to use !!someValue (which is equivalent to Boolean(someValue)) in a boolean context? In other words, in situations like if (someValue) ..., does it make sense to write if (!!someValue) ...?

It hurts my eyes and I think it's redundant, but I want to verify that there aren't any edge cases that require it - because I came across a codebase where this seems to be the default way of checking for truthiness.

Clarification: yes, in the general case !!someValue will convert someValue to a boolean; I'm referring to contexts where the runtime will automatically convert someValue to true or false.

noamtm
  • 12,435
  • 15
  • 71
  • 107
  • Sometimes it's useful outside of if statements, if you need a boolean. – evolutionxbox Feb 08 '22 at 20:35
  • 1
    Of course, but I was asking specifically about boolean context (where the runtime will force the value to boolean anyway). – noamtm Feb 08 '22 at 20:37
  • 3
    I don't think it's useful in a condition, but if you explicitly need a boolean, it matters: `console.log(1 && 2, !!1 && !!2)`. – tevemadar Feb 08 '22 at 20:38
  • Does this answer your question? [What is the !! (not not) operator in JavaScript?](https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript) – shlomieb Feb 08 '22 at 20:38
  • It's a way to ensure boolean instead of truthy/falsey, for example having a value may match truthey but isn't technically `true`, it's just good practice where you want to ensure you have true/false instead of truthey/falsey – Chris W. Feb 08 '22 at 20:38
  • 5
    Your own clarification answers the question =)). You don't need `!!` in boolean context as JS coerces types to boolean implicitly. – Kosh Feb 08 '22 at 20:46
  • 1
    @Kosh I'm in the process of reviewing codebase that use these a lot, I want to verify before I add comments that suggest they remove it. – noamtm Feb 08 '22 at 20:48
  • 1
    Kosh is correct, you can rest assured that won't be the case within conditions; To give you some peace of mind, check out the mdn docs: https://developer.mozilla.org/en-US/docs/Glossary/Truthy – ale917k Feb 08 '22 at 20:56
  • 1
    @noamtm, I love your responsible approach to review! – Kosh Feb 08 '22 at 20:59
  • 1
    I can't think of any scenario where you need `!!` in boolean context. Tested a bunch of data types (including all JS primitives) and I got the same outcome every time. – Tony M Feb 08 '22 at 21:01

3 Answers3

4

That's a helpful way of converting any data type into boolean; The exact synonym would be:

Boolean(someValue)

Some prefer the 'double bang' as being more coincise, while others prefer the classic conversion with Boolean(someValue) for better readability.

So returning to your question, it's most likely redundant in conditions, but it can be handy when needing a conversion from any type to boolean.

ale917k
  • 1,494
  • 7
  • 18
  • 37
  • 1
    Readability is in the eye of the beholder. One could find `!!` better. =)) – Kosh Feb 08 '22 at 20:54
  • Haha so true, quite my preference too, although I worked in the past with teams that made me switch to "increase readability"! – ale917k Feb 08 '22 at 20:58
0

You never need to use a double bang to convert a boolean value to boolean.

boolean === !!boolean
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Can't say I know why anyone would rely on directly checking booleans rather than on truthiness, but there is at least one scenario where truthiness is different than coercing with !!.

console.log([] == false); // true
console.log(!![] == false); // false
if([]) { console.log(true); } // true
if([] == false) { console.log(true); } // true
if(!![] == false) { console.log(true); } else { console.log(false); } // false
Patrick Barr
  • 1,123
  • 7
  • 17
  • 1
    Your exceptional scenario isn't so much about truthiness as it is about the hidden pitfalls of loose equality. An empty array, being an object, is truthy - hence !![] evaluates to true. But double-equal treats it (and a few other surprising things) as false. – Kevin Perry Feb 09 '22 at 00:30