3

In someone else's code I found this way to test the existence of a variable :

if(!!variable) {
  //Do something
} else {
  //Something else
}

Is there a reason to test with if(!!variable) instead of if(variable) ? Is it a good practice?

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • 1
    Why, indeed? Seems less readable to me. Can't wait to see what others say. – duffymo Jun 30 '16 at 09:20
  • Only use of `!!` i can think of is to convert something to `bool`. E.g. `var i = !!0; console.log(i); //false (instead of 0)`. – Arg0n Jun 30 '16 at 09:20
  • 1
    In this particular case it doesn't make any difference, I think. But it's useful when you want to convert an arbitrary value into a boolean (when you later save it to the database, or something) – Sergio Tulentsev Jun 30 '16 at 09:21
  • `if` converts always to boolean. – Nina Scholz Jun 30 '16 at 09:22
  • Some use it as explicit coercion instead of Boolean. Some find it more readable to explicitly tell they want to evaluate a boolean value. – Jacob Jun 30 '16 at 09:28

4 Answers4

6

Is there a reason to test with if(!!variable) instead of if(variable)?

No.

Using if() will coerce the condition to a boolean anyway, there's no need to manually do it. Using this sort of syntax makes sense when you want to guarantee you've got a boolean elsewhere, for example if you wanted to log the truthiness of the expression:

console.log(!!variable)

This will ensure that you get either true or false logged, rather than the original value of the variable. But inside an if? It makes no difference.

James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • It's also useful along with other logical ops: `0 && true` yields 0 for instance. If you expect a boolean value, try: `!!0 && true` –  Jun 30 '16 at 09:32
  • @LUH3417 Indeed. But both of those will behave the same way inside an `if()`, which is what the question was asking. – James Thorpe Jun 30 '16 at 09:36
0

No, in the case of a if, their is usually no reason to write that in JS.

I would not call it bad practice, since it can often come from programmer coming from strongly typed language, and is a common quick way to do the conversion to bool in thoses language.

It can also be used to emphasise the importance of the truthiness of the value in a long if, and show the programmer didn t forget to put the condition.

TL:DR: As for a lot of things: it may not be good practice, but it only become bad practice if your team is not used to that practice, either train them, document, or leave it.

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
-2

Double negation is a bit of a hacky way (IMHO) to convert a variable to bool. using ! on variable coerces it to a bool and negates it, and using it again convert it back to the 'correct' value. So !!0 === false

As mentioned by James there is no reason to do this inside an if statement. The only reason really to do this would be to coerce to a bool for logging purposes or similar so the value logged is true or false rather than 0, an empty string etc

Anduril
  • 1,236
  • 1
  • 9
  • 33
-3

Double ! (!!) is not an operator. It's just ! twice - double negation.

André Senra
  • 1,875
  • 2
  • 11
  • 13