Beware the leading zero in Javascript parseInt()!

Exhibit A,

parseInt('01'); //equals 1
parseInt('02'); //equals 2
parseInt('03'); //equals 3
parseInt('04'); //equals 4
parseInt('05'); //equals 5
parseInt('06'); //equals 6
parseInt('07'); //equals 7
parseInt('08'); //equals 0 !!
parseInt('09'); //equals 0 !!

When Javascript encounters a leading zero it assumes an octal number. So when it sees 08 or 09 then there are problems.

The fix is to add the radix to the function call,

myInt = parseInt('08',10); //equals 8

Leave a Reply

Your email address will not be published. Required fields are marked *