Convert boolean result into number/integer

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

    I created a JSperf comparison of all suggested answers.

    TL;DR - the best option for all current browsers is:

    val | 0;
    

    .

    Update:

    It seems like these days they are all pretty identical, except that the Number() function is the slowest, while the best being val === true ? 1 : 0;.

    0 讨论(0)
  • 2020-12-02 06:22

    if you want integer x value change if 1 to 0 and if 0 to 1 you can use (x + 1) % 2

    0 讨论(0)
  • 2020-12-02 06:23

    I was just dealing with this issue in some code I was writing. My solution was to use a bitwise and.

    var j = bool & 1;
    

    A quicker way to deal with a constant problem would be to create a function. It's more readable by other people, better for understanding at the maintenance stage, and gets rid of the potential for writing something wrong.

    function toInt( val ) {
        return val & 1;
    }
    
    var j = toInt(bool);
    

    Edit - September 10th, 2014

    No conversion using a ternary operator with the identical to operator is faster in Chrome for some reason. Makes no sense as to why it's faster, but I suppose it's some sort of low level optimization that makes sense somewhere along the way.

    var j = boolValue === true ? 1 : 0;
    

    Test for yourself: http://jsperf.com/boolean-int-conversion/2

    In FireFox and Internet Explorer, using the version I posted is faster generally.

    Edit - July 14th, 2017

    Okay, I'm not going to tell you which one you should or shouldn't use. Every freaking browser has been going up and down in how fast they can do the operation with each method. Chrome at one point actually had the bitwise & version doing better than the others, but then it suddenly was much worse. I don't know what they're doing, so I'm just going to leave it at who cares. There's rarely any reason to care about how fast an operation like this is done. Even on mobile it's a nothing operation.

    Also, here's a newer method for adding a 'toInt' prototype that cannot be overwritten.

    Object.defineProperty(Boolean.prototype, "toInt", { value: function()
    {
        return this & 1;
    }});
    
    0 讨论(0)
  • 2020-12-02 06:27

    The typed way to do this would be:

    Number(true) // 1
    Number(false) // 0
    
    0 讨论(0)
  • 2020-12-02 06:27

    The unary + operator will take care of this:

    var test = true;
    // +test === 1
    test = false;
    // +test === 0
    

    You'll naturally want to sanity-check this on the server before storing it, so that might be a more sensible place to do this anyway, though.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题