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?

6
  • 1
    Why, indeed? Seems less readable to me. Can't wait to see what others say. Commented Jun 30, 2016 at 9: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). Commented Jun 30, 2016 at 9: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) Commented Jun 30, 2016 at 9:21
  • if converts always to boolean. Commented Jun 30, 2016 at 9: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. Commented Jun 30, 2016 at 9:28

4 Answers 4

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.

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

2 Comments

It's also useful along with other logical ops: 0 && true yields 0 for instance. If you expect a boolean value, try: !!0 && true
@LUH3417 Indeed. But both of those will behave the same way inside an if(), which is what the question was asking.
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.

Comments

-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

5 Comments

What is the difference between if(0) and if(!!0)
@SergioTulentsev There is no difference really because if coerces the value anyway.
This was my attempt at hinting that your answer doesn't answer the actual question.
@SergioTulentsev Fair enough. I misunderstood the question, but have tried to expand a bit (for what its worth)
I see that and I retracted my downvote. :)
-3

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

2 Comments

But why use it specifically here? It's not like it's being assigned to a variable that you want to guarantee is a bool.
How does this answer the question? JFYI, the question isn't "what is this !! operator and what does it do?".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.