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?

Community
  • 1
  • 1
sum2000
  • 1,363
  • 5
  • 21
  • 36

3 Answers3

3

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

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
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.

slolife
  • 19,520
  • 20
  • 78
  • 121
0

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