I’ve just came back from Wurbe, where I’ve enjoyed a late evening snack (consisting of beer, Pepsi and pizza) and chated with other fellow developers on a large range of subjects, raging from current trends in development to Klingon grammar.
Bodgan Lucaciu told us a funny story about a strange bug. It’s like this: in Javascript, when you parse a number from a string – let’s say a month’s index like 07 – with parseInt you must be very careful, because, for parseInt, the leading 0 is an indicator that the number is written in octal instead of decimal.
alert(parseInt('07')); // for July - will echo 7
alert(parseInt('08')); // for August - will echo 0 as there is no 08 in octal
So if your application mysteriously stops working on the first of August, this might be your problem. Although this “feature” is marked as deprecated, it’s still present in many modern browsers, so one can never be too careful. Also keep in mind that by default, numbers starting with 0x are considered to be hexadecimals.
The JavaScript parseInt function accepts the second argument (radix), which shuld be the a number representing the numeral system to be used.
Yes, it would be a good idea to always add 10 as a radix when using parseInt. But there are some scripts – most of them – that don’t specify the radix, one might just run into trouble at the beginning of August
It’s always necessary to specify the radix in order to avoid this situation. Danny Goodman (author of JS Bible and Dynamic HTML: the definitive reference, etc.) says: “An optional second parameter to parseInt function enables you to specify the base of the number represented by the string. This comes in handy particularly when you need a decimal number from a string that starts with one or more zeros. Normally, the leading zero indicates an octal value. But if you force the conversion to recognize the string value as a decimal, it is converted the way you expect:” – excerpt from JS Bible 6th Ed.