Double not (!!) vs type coercion in JavaScript [duplicate]
Asked Answered
H

2

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?

Hippogriff answered 10/6, 2012 at 20:12 Comment(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.Deenadeenya
I know exactly what the !! pseudo operator does, that is not what I asked.Hippogriff
There is no real use, it's a bad habit having for pretext readability.Deenadeenya
You've got me there, so now the only question is that of why it is in such common use.Hippogriff
@watkinsj. I've never seen it in a real serious library (like jQuery...).Bilander
The question referenced by creemama explicitly states that it is used in jQuery...Hippogriff
@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.Drugge
F
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.

Furgeson answered 10/6, 2012 at 20:17 Comment(1)
Accepted as best, would like to point out @Drugge some APIs may ===/typeof to require boolean values.Hippogriff
B
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)
Bilander answered 10/6, 2012 at 20:15 Comment(7)
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.Hippogriff
@watkinsj. I believe I answered that question in the first line...Bilander
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.Hippogriff
@watkinsj. There are none, this why I didn't wrote them...Bilander
@watkinsj It is simply a bad practice. Bad practices have a tendency of getting widely used. No deeper meaning than this.Connelley
@lanzz. Pretty funny, the answer is there is none, but he's not willing to accept that fact.Bilander
@Bilander This answer was "You don't need to use !! inside an if expression." The question's answer was that there are instances in which it could be useful shorthand when a framework or other code checks types.Hippogriff

© 2022 - 2024 — McMap. All rights reserved.