+!! operator in an if statement

后端 未结 6 1989
旧巷少年郎
旧巷少年郎 2020-12-29 18:31

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

6条回答
  •  孤城傲影
    2020-12-29 19:11

    ! 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.

提交回复
热议问题