Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
I would think it has something to do with octal parsing, since it only happens on 8 or 9. There was also the thought that this was a Chrome bug, but it replicates in Firefox as well.
Is this intentional behavior? If so, why?
The solution here is simple. NEVER call
parseInt()without specifying the desired radix. When you don’t pass that second parameter,parseInt()tries to guess what the radix is based on the format of the number. When it guesses, it often gets it wrong.Specify the radix like this and you will get the desired result:
As to what rules it uses for guessing, you can refer to the MDN doc page for
parseInt().So, according to these rules,
parseInt()will guess that"08"is octal, but then it encounters a digit that isn’t allowed in octal so it returns0.When you pass a number to
parseInt(), it has nothing to do because the value is already a number so it doesn’t try to change it.