JavaScript parseInt method - be careful
Today I solved a bug in JavaScript for a company I am advising to. The bug had to do to the simple fact that they did not really understood the ParseInt method. The special thing to notice about this method is the radix (The base ) in which the method works with.
As it turns out, the parseInt method not only gets a string as a parameter and tries to convert it to integer, it can also get a second optional parameter which is the radix. (from 2 to 36). Why is this radix so important? Simple. Because if the string begins with "0" then the function will return "0" no matter what number follows it, this is because the default radix in this case is 8(octal).
So for example:
var sNumber = "8";
alert(parseInt(sNumber));
will alert "8"
var sNumber2 = "08";
alert(parseInt(sNumber2));will alert "0".
The last line should have been written like this: alert(parseInt(sNumber2,10));
So next time you are trying to convert a string into int - pay attention.