A fact about JavaScript parseInt() that i wasn’t aware of, or why web projects sometimes break on August

JavaScript, is a really, REALLY cool and powerful language (especially if used correctly), and i intend to write about it a lot in the future.
Bur right now, i want to mention a fact about JavaScript parseInt(), that i wasn’t aware of.

First of all, parseInt() function is used to parse numbers from strings into javascript integer type (that is, parseInt(’1′) would give 1), like it wouldn’t be clear :)

But i don’t think that everyone knows (at least i didn’t know this), that it takes an optional second parameter -  radix. Radix represents the numeral system to be used when parsing the integer. It defaults to 10, so parseInt() tries to parse the string as a decimal number by default.

However, there are exceptions to this (taken from w3schools.com JavaScript parseInt() documentation):
If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with “0x”, the radix is 16 (hexadecimal)
  • If the string begins with “0″, the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)


So, if the number in the string begins with a zero (like months do, in standard date format YYYY-MM-DD), and you pass it to parseInt() without explicitly specifying radix to be 10, it will interpret it as an octal number. So, parseInt(’08′), will result in 0, as will parseInt(’09′), because digits 8 and 9 do not exist in the octal numbering system, so parseInt will return 0, because it won’t be able to parse the numbers from the strings.

If you plan to use decimal numbers with leading zeroes together with parseInt, either specify the radix:

parseInt(StringValue, 10)

Or use a different method instead of parseInt, like zero subtraction for example:

integerValue = StringValue – 0

This works too, and it works in all browsers. However, don’t try to use it like this: StringValue + 0, because that way the zero may just get appended at the end of the string (plus operator is used to append strings to one another in JavaScript, remember?). You have to use subtraction, that way the JavaScript interpreter will know for sure, that it is dealing with numbers, because there is no way to reduce one string, using another.

Because of this, a piece of code i wrote for one project suddenly stopped working on August (that is, the eighth month of the year). The bug that i left in my code was that i split a date from the format YYYY-MM-DD into an array of year,month,day and then i would use parseInt() to get the number of the month. It worked well for months 01,02,03,04,05,06 and 07 because these octal numbers represent the same decimal numbers, but it failed for 08, because as i’ve mentioned above, parseInt() would return 0 instead of 8.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)
Loading...

3 responses to “A fact about JavaScript parseInt() that i wasn’t aware of, or why web projects sometimes break on August”

  1. Gilberto

    I read a lot of interesting articles here. Probably you spend
    a lot of time writing, i know how to save you a lot of
    time, there is an online tool that creates high quality,
    SEO friendly articles in minutes, just type
    in google – laranitas free content source

  2. Nickolas

    It’s hard to find your posts in google. I found it on 20
    spot, you should build quality backlinks , it will help you to increase traffic.

    I know how to help you, just type in google – k2 seo tips

  3. Beemer

    Thanks for this explanation. I just hit this same issue and this solved my problem.

Leave a Reply