Convert boolean result into number/integer

前端 未结 18 2122
刺人心
刺人心 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:28

    Use the unary + operator, which converts its operand into a number.

    + true; // 1
    + false; // 0
    

    Note, of course, that you should still sanitise the data on the server side, because a user can send any data to your sever, no matter what the client-side code says.

提交回复
热议问题