parseInt vs unary plus, when to use which?

后端 未结 5 607
终归单人心
终归单人心 2020-11-22 09:50

What are the differences between this line:

var a = parseInt(\"1\", 10); // a === 1

and this line

var a = +\"1\"; // a ===          


        
5条回答
  •  眼角桃花
    2020-11-22 10:14

    #Please see this answer for a more complete set of cases




    Well, here are a few differences I know of:

    • An empty string "" evaluates to a 0, while parseInt evaluates it to NaN. IMO, a blank string should be a NaN.

        +'' === 0;              //true
        isNaN(parseInt('',10)); //true
      
    • The unary + acts more like parseFloat since it also accepts decimals.

      parseInt on the other hand stops parsing when it sees a non-numerical character, like the period that is intended to be a decimal point ..

        +'2.3' === 2.3;           //true
        parseInt('2.3',10) === 2; //true
      
    • parseInt and parseFloat parses and builds the string left to right. If they see an invalid character, it returns what has been parsed (if any) as a number, and NaN if none was parsed as a number.

      The unary + on the other hand will return NaN if the entire string is non-convertible to a number.

        parseInt('2a',10) === 2; //true
        parseFloat('2a') === 2;  //true
        isNaN(+'2a');            //true
      
    • As seen in the comment of @Alex K., parseInt and parseFloat will parse by character. This means hex and exponent notations will fail since the x and e are treated as non-numerical components (at least on base10).

      The unary + will convert them properly though.

        parseInt('2e3',10) === 2;  //true. This is supposed to be 2000
        +'2e3' === 2000;           //true. This one's correct.
      
        parseInt("0xf", 10) === 0; //true. This is supposed to be 15
        +'0xf' === 15;             //true. This one's correct.
      

提交回复
热议问题