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 ~~
?
The table in thg435's answer I believe is comprehensive, however we can summarize with the following patterns:
true
to 1, but"true"
toNaN
.parseInt
is more liberal for strings that are not pure digits.parseInt('123abc') === 123
, whereas+
reportsNaN
.Number
will accept valid decimal numbers, whereasparseInt
merely drops everything past the decimal. ThusparseInt
mimics C behavior, but is perhaps not ideal for evaluating user input.parseInt
, being a badly designed parser, accepts octal and hexadecimal input. Unary plus only takes hexademical.Falsy values convert to
Number
following what would make sense in C:null
andfalse
are both zero.""
going to 0 doesn't quite follow this convention but makes enough sense to me.Therefore I think if you are validating user input, unary plus has correct behavior for everything except it accepts decimals (but in my real life cases I'm more interested in catching email input instead of userId, value omitted entirely, etc.), whereas parseInt is too liberal.
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 a0
, whileparseInt
evaluates it toNaN
. IMO, a blank string should be aNaN
.The unary
+
acts more likeparseFloat
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.
.parseInt
andparseFloat
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, andNaN
if none was parsed as a number.The unary
+
on the other hand will returnNaN
if the entire string is non-convertible to a number.As seen in the comment of @Alex K.,
parseInt
andparseFloat
will parse by character. This means hex and exponent notations will fail since thex
ande
are treated as non-numerical components (at least on base10).The unary
+
will convert them properly though.The ultimate whatever-to-number conversion table:
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 ;)
Be carefull, parseInt is faster than + unary operator in Node.JS, it's false that + or |0 are faster, them are faster only for NaN elements.
Check this out: