parseInt vs unary plus, when to use which?

What are the differences between this line:

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

and this line

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

This jsperf test shows that the unary operator is much faster in the current chrome version, assuming it is for node.js!?

If I try to convert strings which are not numbers both return NaN:

var b = parseInt("test" 10); // b === NaN
var b = +"test"; // b === NaN

So when should I prefer using parseInt over the unary plus (especially in node.js)???

edit: and what’s the difference to the double tilde operator ~~?

6 Answers
6

Leave a Comment