On some platforms, the builtin function parseInt parses strings starting with the digit 0 as octal values (unless an explicit radix is provided). This can lead to unexpected results when parsing decimal numbers that may be zero-padded, such as dates.

Provide an explicit radix as the second parameter to parseInt.

In the following example, parseInt is used to convert the contents of a field in an HTML form to a number:

Now assume that a user has entered a zero-padded decimal number, say 09, into the form. Since the first digit is a zero, older versions of parseInt interpret this value as an octal number. When they then encounter 9 (which is not an octal digit), they will stop parsing and discard the rest of the string, returning the value 0, which is probably not what was expected.

To avoid this problem, an explicit radix parameter should be parsed as follows:

  • D. Crockford, JavaScript: The Good Parts, Appendix A.7. O'Reilly, 2008.