+!! operator in an if statement

后端 未结 6 2015
旧巷少年郎
旧巷少年郎 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:09

    !! converts a value to a boolean (true or false). + then converts that boolean to a number, either 1 for true or 0 for false.

    > +true
    1
    > +false
    0
    

    Personally I find it clearer to write something like this, when dealing with two booleans:

    if (!config.template == !config.templateUrl) {
      throw ...
    }
    

    Code clarity and readability be damned, apparently.

提交回复
热议问题