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
  • 13,139
  • 14
  • 57
  • 108

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,419
  • 7
  • 38
  • 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,064
  • 7
  • 18
  • 22