2

As in title.

let x = anything;

if(x){
  console.log("True");
}

if(!!x){
  console.log("True");
}

Output here is always going to be True True ?

user2864740
  • 54,112
  • 10
  • 112
  • 187
UnderNotic
  • 335
  • 5
  • 15
  • 1
    `!!` ensures a boolean, as sometimes returned items are undefined, or null and you want to make sure the value is exactly T or F. – Fallenreaper Sep 13 '16 at 18:54
  • The only difference would be on the programming side. I don't think it matters for JS and, in fact, I think I checked it once and it didn't affect performance at all. However, future maintaners of the code would know that `x` is not a boolean. – VLAZ Sep 13 '16 at 18:55
  • 3
    *In JavaScript*, there is no logic difference between `x` and `!!x` when used as the expression to an `if` statement. This does not hold for other usages of the result; or for all other languages. – user2864740 Sep 13 '16 at 19:00
  • 1
    @Fallenreaper well, the words you say are correct, but in the example OP has, the result would be exactly the same for any value of `x`. However, yes - sometimes you might want to transform an input into an explicit boolean. – VLAZ Sep 13 '16 at 19:03
  • 1
    It's just a shortcut to convert to boolean. Practical use of `!!` is basically when you need to save something into the object field (or simple var). i.e. `obj.isThisTrue = !!isVarExist` (and for example send `obj` with bool field to the server) – Sergei Panfilov Sep 13 '16 at 19:04

0 Answers0