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

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?

Participa en la discusión

nice addition

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



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!

Because, the % gives you the

Because, the % gives you the remainder, not the integer. Also you can use the % operator on strings, but you can use parseInt to get a integer from a string.

That was helpful !! what a

That was helpful !! what a catch !!
parseInt('08') => "zero" !! now I understand.

This nearly drove me insane!

This nearly drove me insane! The solution I found though is that when comparing, you don't need parseInt or a % at all...

if ('08' = 8) seems to work fine!
(in IE, FF, and Chrome)

That seems very strange to me; using parseInt('08',10) seems more proper, but I am reluctant to change it again now that it is working.

I can't believe parseInt is so bad... just because 09 doesn't exists in octal does not make returning 0 any better then returning 9. How hard is it to make the function use base 10 even with a leading 0 whenever there is an 8 or a 9 anywhere within the string? This is ultimate failure on the part of who ever invented that javascript base-function and who ever maintains the javascript standard that modern browsers conform to!

Excellent!!! Thanks very

Excellent!!! Thanks very much.

This had been driving me

This had been driving me crazy for the better part of the day until I tracked it down through process of elimination. Given my code I decided to use parseFloat() instead, but I like your way too.

parseInt('08') is not 0

parseInt('08') is not 0 because 8 in octal is 0, but because its not a valid octal number. Therefore parseInt('09') is also 0, for the same reason, although it equals 1 in octal ;)

I had gone through the first

I had gone through the first 20 days of 2months, and checked this in 4 browsers.

bit of a silly default. most of the world operates in decimal format.

but thanks for your post.

Actually parseInt("123456")

Actually parseInt("123456") is slower than "123456"<<0

  • it's shorter
  • "08"<<0 will be 8

the test on my machine: [looptime, result]
[[388ms, -901237485], [331ms, -901237485], [330ms, -901237485], [334ms, -901237485]]

s="-901237485"; n=100000; x=y=z=a=0;
d1=new Date().getTime();
for (i=0;i<n;i++)x=parseInt(s,10);
d2=new Date().getTime();
for (i=0;i<n;i++)y=s<<0;
d3=new Date().getTime();
for (i=0;i<n;i++)z=s>>0;
d4=new Date().getTime();
for (i=0;i<n;i++)a=s|0;
d5=new Date().getTime();

Great tip thank you very

Great tip
thank you very much

I remember having this issue

I remember having this issue a couple of years ago and in a new project, ran across it again. Yours was the first result in my Google search and it was a spot on fix... thank you!

Thank your for this tip. I

Thank your for this tip. I was very confused about this problem. First i started if-clauses with if(i = "0-1")... oh man :D

This helped me today :)

This helped me today :) Thanks...

You lifesaver - this annoyed

You lifesaver - this annoyed me so much ! :D

Thank you thank you thank you :D

Thank you so much :D

Thank you so much :D



First off let me say thank

First off let me say thank you. I would like to spend 15 hours punching the person in the face who thought of this for wasting everyone's time and making programming harder for no reason. Use defaults!! Everyone expects them. If the pattern of parsInt(every number and every patter except 08 and 09) works why would this one exception make ANY sense??? People use javascript for parsing everyday text. It is the language of the web. We aren't crunching binary. It is a fringe case by FAR. I've lost hours on this problem because there is no way I thought it could be in the language. It isn't even on the W3C website. Thank you so much for pointing this bug out.




Publica comentarios relevantes, usa español correcto y no envíes publicidad. Creemos discusiones en las que valga la pena participar. Puedes usar Markdown.

Tu comentario