Convert boolean result into number/integer

前端 未结 18 2120
刺人心
刺人心 2020-12-02 05:21

I have a variable that stores false or true, but I need 0 or 1 instead, respectively. How can I do this?

18条回答
  •  一个人的身影
    2020-12-02 06:13

    +!! allows you to apply this on a variable even when it's undefined:

    +!!undefined    // 0
    +!!false        // 0
    +!!true         // 1
    
    +!!()  // 1 if it evaluates to true, 0 otherwise
    

提交回复
热议问题