-2

What is the difference between these two checks?

if (obj.string) { ... }

if (!!obj.string) { ... }

Thanks

shtuken
  • 171
  • 1
  • 9
  • 1
    Related: https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript When used as a conditional, there is no logical difference. – Alex Wayne Oct 28 '20 at 18:10
  • 1
    Since the question is 'what is the difference', then that would be two extra explicit operations (!!) vs the first version which just does the truthy check. – Taplar Oct 28 '20 at 18:12
  • 1
    @Taplar, Depends. A smart compiler could optimize the double-negation away. – ikegami Oct 28 '20 at 18:13
  • Very true. So there's that. – Taplar Oct 28 '20 at 18:14

1 Answers1

1

!! is used to normalize a "truthy" value into true or false. There's no point in doing this here. if is just as capable of evaluating the truth of the value as ! is.

ikegami
  • 367,544
  • 15
  • 269
  • 518