Javascript parseInt gotcha (All browsers)

I wanted to keep this blog to put up interesting gotchas that I’ve found whilst programming. This is definitely one of the craziest I’ve seen.

I had a problem with my jquery calendar where I was parsing a string as an int. I was calling this:

parseInt("08");

So you would assume an int of 8 yeah?
The actual returned value is ZERO, which was a bit of a shock to me. Apparently the javascript parseInt function will take a string leading with a zero as an octal number. To force it to read a base 10 number you will have to call

parseInt("08", 10);

See this post for a great explanation http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604. Its amazing how long I have been programming in javascript and have never picked up this one..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s