1

While reading lodash source code, I saw:

this.__chain__ = !!chainAll;

Why would one use !! on the chainAll parameter?

I assume this is a safer way to detect falsy values or dealing w/ different JavaScript versions, but would like to know the scenario it protects.

Whymarrh
  • 11,635
  • 13
  • 55
  • 96

2 Answers2

4

The !! construct is a simple way of turning any JavaScript expression into its Boolean equivalent. For exmaple: !!"something" === true, while !!0 === false

DEMO

nanobash
  • 5,063
  • 6
  • 33
  • 56
0

JS use dynamic type for variable. Thanks to this trick you convert the variable into a boolean if it was not a boolean, an it doesn't modify the value if it was a boolean

Gwenc37
  • 2,038
  • 7
  • 16
  • 22