parseInt()
without the second parameter radix. This might cause a problem when the variable passed to parseInt() starts with a 0, which makes JavaScript interpret the value as an octal number. Or if the string even starts with “0x” parseInt()
might come up with the idea to see a hexadecimal number.
>>> parseInt("8")
8
>>> parseInt("08")
0
>>> parseInt("010") // A juicy mistake, octal numbers.
8
>>> parseInt("0x10") // Probably rare, but possible, hexadecimals.
16
>>> parseInt("08", 10) // Prevent problems, use the radix.
8
>>> parseInt("010", 10)
10
And if the paramter passed to
parseInt()
is a variable that comes from some other place you can not be sure that the string does not start with a “0″. So using the radix might save a lot of headache.