0

What will be difference when we check a value with !! or without it.

Example:-

a) if(!!Value) { do this } => I know this condition will return a boolean

b) if(Value) { do this } ==> This will check if value has anything inside it

But what will be difference in their usage? If not above case, in what case they both will make a difference ?

  • 1
    https://stackoverflow.com/q/784929 I'm pretty sure the `!!` is completely pointless, since `if` will already check to see if it's truthy/falsey – CertainPerformance Apr 28 '20 at 22:38
  • 2
    There is no difference. Both will be evaluated the same way. `if (!!Value)` makes it slightly more clear that `Value` is not a boolean to begin with, so it might be useful for letting people who work on that code what to expect but ultimately, it doesn't matter, both are acceptable. – VLAZ Apr 28 '20 at 22:38

1 Answers1

0

There is no difference using if(value) or if(!!value) in JavaScript or in TypeScript.

!! will coerce any following value to Boolean type.

However, an expression inside a if will be coerced to Boolean by JavaScript / TypeScript anyway.

In JavaScript the values that will be coerced to false ("falsy values") are false, 0, "", NaN, null, undefined.

Any other is truthy (including empty arrays and objects), i.e. will be coerced to true.

Pac0
  • 16,761
  • 4
  • 49
  • 67