parseInt vs unary plus, when to use which?

后端 未结 5 550
终归单人心
终归单人心 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:08

    Consider performance too. I was suprised that parseInt beats unary plus on iOS :) This is helpful for web apps with heavy CPU consumption only. As a rule-of-thumb I'd suggest JS opt-guys to consider any JS operator over another one from the mobile performance point of view nowadays.

    So, go mobile-first ;)

提交回复
热议问题