Wednesday, October 8, 2008

Javascript: parseInt('08'), parseInt('09') return 0

Function parseInt(string, radix) has two arguments.
if you don't give value to "radix", Javascript will not necessary to do a base 10 conversion. Because the '08' starts with '0', javascript actually thinks it's a hexadecimal value. So it will do parseInt('08', 16).

To avoid confusion, use parseInt('08', 10), which will return 8.

No comments: