I just noticed that:
//IN CHROME JS CONSOLE
parseInt("03010123"); //prints: 3010123
//IN NODE.JS
parseInt("03010123"); //prints: 790611
Since both are based on V8, why same operation yielding different results???
Number() converts the type whereas parseInt parses the value of input. As you see, parseInt will parse up to the first non-digit character. On the other hand, Number will try to convert the entire string.
The parseInt function takes in at most two arguments. The first argument is a string containing a number, and the second argument is a radix that determines the numeral system of the number present in the string. The radix is an optional parameter.
Undefined behavior occurs when the string being passed to parseInt has a leading 0, and you leave off the radix parameter.
An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.
Some browsers default to base 8, and some to base 10. I'm not sure what the docs say about Node, but clearly it's assuming base 8, since 3010123
in base 8 is 790611
in base 10.
You'll want to use:
parseInt("03010123", 10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With