I was reviewing the code for an angularjs factory to better understand how it works. The code contains an if
statement that I don\'t fully understand.
I
! is the logical not operator. It is a unary operator that converts its operand to a boolean and then negates it. !!
is just that operator twice, the second !
undoes the negation, so the end result is just conversion to a boolean.
+ is the unary plus operator, which converts it's operand to a number. In the case of a boolean, false becomes 0
, and true becomes 1
.
So, +!!(expression)
evaluates to 1
if the expression is truthy and 0
if the expression is falsy.