0

Possible Duplicate:
Workarounds for JavaScript parseInt octal bug

I was learning the parseInt() function of javascript and was just trying out, and out of nowhere

parseInt('08')  returns 0

moreover,

parseInt('07')  returns 7 //which is correct

but again

parseInt('09')   returns 0 // really, are you kidding me.?

Either I am crazy or I am missing something?

6

3 Answers 3

3

Its because its doing octal when the string starts with 0.

You should pass the radix of 10 as the second parameter.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to specify the radix:

parseInt('08', 10);  // base 10 radix

Running your javascript thru JSLint will call this out as well.

From the documentation, parseInt parses the string as octal when the string starts with a 0, if no radix is specified.

Comments

0

No, you're. Only parseInt obeys rules for octal numbers and applies them when the string begins with 0.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.