5

Is there any advantage, except indicating an explicit conversion, to using a double not operator in JavaScript? It often seems that these days, people like to check for existence of new APIs using the double not, but I have never, ever read any advantage to it.

if(!!window.File) // The File API is supported. else // Your browser sucks. 

The one thing that I have read is that it is a concise, obscure way to type cast to Boolean, however, when used in this context the object will be auto coerced to Boolean anyway since we are checking to see if it is defined.

In short, why do people do two Boolean operations on top of the engine's?

7
  • As demonstrated by the link, it globally make a conversion to a boolean. Here it's mainly intended as making the code more readable, meaning that only the boolean value is needed. Commented Jun 10, 2012 at 20:15
  • I know exactly what the !! pseudo operator does, that is not what I asked. Commented Jun 10, 2012 at 20:16
  • 2
    There is no real use, it's a bad habit having for pretext readability. Commented Jun 10, 2012 at 20:17
  • You've got me there, so now the only question is that of why it is in such common use. Commented Jun 10, 2012 at 20:26
  • 1
    @watkinsj it's useful for APIs that insist on an actual boolean true or false value (like some jQuery APIs), and not just a value to be cast later. Sometimes APIs with optional parameters use typeof to figure out what a particular set of parameters is supposed to mean, so if it's necessary to pass in a real boolean, !! is a quick (and perfectly safe) way to do it. It's idiomatic, and any experienced JavaScript developer should be familiar with the idiom. Commented Jun 10, 2012 at 21:08

2 Answers 2

7

I don't really see any reason to do it in context like you present. It will not affect the result in any way.

The only time I'd see this as a good idea is if you're building a function which is supposed to return a bool value, and you need to cast it from some other value, eg...

function isNotFalsy(val) { return !!val; } 

The example may be a bit forced, but you get the idea. You would always want to make sure the return value is of the type the user would expect.

Sign up to request clarification or add additional context in comments.

1 Comment

Accepted as best, would like to point out @Pointy some APIs may ===/typeof to require boolean values.
1

You don't need to use !! inside an if expression. It's being used the convert the value to a Boolean, and the if does it by default.

var x = ""; // A falsy value !x // true !!x // false if (x) === if (!!x) 

7 Comments

Thanks for answering, but I know this. The question was if there is any advantage to using it in an if statement since it is such common practice.
@watkinsj. I believe I answered that question in the first line...
You said it is unnecessary. I pointed this out in the question. I don't see any answer as to any advantages or explanation for it's proliferation inside the if statement.
@watkinsj. There are none, this why I didn't wrote them...
@watkinsj It is simply a bad practice. Bad practices have a tendency of getting widely used. No deeper meaning than this.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.