3

Some JavaScript examples use !! to check if an object is available

// Check to see if Web Workers are supported
if (!!window.Worker) {
  // Yes, I can delegate the boring stuff!
}

Why is this preferred to just if (window,Worker) and in what context would this fail?

yannis
  • 694
  • 11
  • 17

3 Answers3

3

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; 
2540625
  • 11,022
  • 8
  • 52
  • 58
2

It isn't.

!! is useful when you are passing an object into a function that (like jQuery's toggle()) checks to see if its argument is a boolean, or when the function might stash its argument and thereby prevent the object from being collected.

The case you cite is neither of those. Use a bare if.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144
1

In the condition of an if statement both versions will do the same thing and its a stylistic choice. Some people prefer use !! to highlight that Worker is not a boolean and is being converted tto boolean.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 1
    I think a six-year-old child understands that the thing after the word "if" is to be interpreted as true or false. `!!` is just showing off. – Michael Lorton Jul 19 '14 at 09:03
  • 1
    @Malvolio: The purpose of the `!!` is to make the reader aware that the argument *isn't* a Boolean value, but is being coerced into one. – supercat Jul 23 '15 at 23:41
  • @supercat -- it would certainly have that effect, but why would the reader care? – Michael Lorton Jul 24 '15 at 01:13
  • @Malvolio: A reader who sees `if (woozle(x,y))...` and also sees `fnord = woozle(x,y);`, might expect that `fnord` would be given a true/false value. Changing the code to `if (!!woozle(x,y))` would suggest to the reader that `woozle` is probably returning something other than a true/false value, and thus avoid creating any false expectations about `fnord = woozle(x,y);`. – supercat Jul 24 '15 at 15:38
  • @supercat -- I think your reasoning is sound but your assertions are in error. Far more programmers know how the `if` statement works than know what `!!` means. In fact, I would doubt that there are more than a handful of Javascript programmers in the whole world who instinctively assume that the argument to the if statement should be a boolean but have a clue what bang-bang does. – Michael Lorton Jul 24 '15 at 17:09
  • @Malvolio: What I was originally saying when I wrote this answer is that the `!!` can be used to mitigate the fact that Javascript is dynamically typed and does not have type annotations. This has nothing to do with how the if statement works. – hugomg Jul 24 '15 at 18:43