What? parseInt(’08’) returns 0 with Javascript? WHY!

December 12, 2012

Turn a string into a number in Javascript is just

var string2Number = "08"; parseInt(string2Number)

This works for just about everything…. except “08” and “09”.

The leading zero in the string tells the Javascript engine that it is an octal number. Because 8 and 9 are not valid numbers in octal, parseInt returns 0. This is expected behavior because they are not valid octal integers and parseInt returns 0 because the first valid number encountered is a zero. – Resource

So, since they aren’t octal you need to provide a base parameter. This is just good practice regardless, so make a habit of doing this and you’ll never run into this problem.

parseInt("08", 10)

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive