Why does parseInt('08') return 0 in Javascript?

21 August 2012

When passing a value padded with a 0, mostly when working with time, the Javascript parseInt() function returns 0 when the value is higher than 07. Only "08" and "09" return a value of zero. 01 to 07 works fine.

parseInt('08');  // returns 0
parseInt('04');  // returns 4
parseInt('02');  // returns 2
parseInt('09');  // returns 0

Why is this? The parseInt function assumes that the value passed is an octal number. The octal system goes from 0 to 7. To tell the parseInt() function what to do, you need to use the second parameter of the function. This is the radix value (2-36) which represents the numeral system to be used. We all use and think in the decimal (10) numeral system, so this fixes your problem:

parseInt('08',10);  // returns 8
You must have JavaScript enabled to use this form!

Leave a comment!

  1. Your mail is safe with me. It's only only used to display your Gravatar image!

1 comment

  1. Gravatar

    jos butler

    18 July 2018

    Javascript parseInt() function return zero for 8 and 9 and rest are working fine. You can use the second parameter of the function, radix value which takes value from 2 to 36. From https://supportnumbers.net/aol-technical-support/ you get to know about Javascript parseInt() function.