Why Javascript Returns Zero For parseInt('08') and parseInt('09')?

Grab the feed

School is a twelve-year jail sentence where bad habits are the only curriculum truly learned.

John Taylor Gatto

Recent comments

Writing a simple Javascript function to create a Date object from a 'yyyy-mm-dd' string I found a small, but important, issue with the parseInt function, used to transform strings to integers.

Both parseInt('08') and parseInt('09') return zero because the function tries to determine the correct base for the numerical system used. In Javascript numbers starting with zero are considered octal and there's no 08 or 09 in octal, hence the problem.

To fix this just add the second parameter for parseInt, the base to be used for the conversion. The correct calls should be parseInt('08', 10) and parseInt('09', 10).

Another of those little details, uh?

Join the conversation

nice addition

thanks for the tip! im always using the php version for creating data object, but this is certainly handy too.

Regards,

Aislin

Good Tip.

Good Tip.

This is very good thing to

This is very good thing to catch these type of special cases. Thank you very much.

My problem has been resolved.

My problem has been resolved. Thank you very much.

why we no need of parseint

why we no need of parseint when we use "%" operator?
it executes without using parseint.
Anybody please reply.

Holy smokes, I was going

Holy smokes, I was going crazy with a deep mysterious error until I tracked it down to parseInt('08') and parseInt('09') returning zero. Thanks for the explanation and fix!

Keep your comments relevant, written in good English and don't spam. Let's create useful and valuable discussions. Markdown is welcome.

Add your comment