JavaScript offers us a suite of functions for assisting the conversion from strings to numbers. While often done implicitly, it is good practice to perform the casts explicitly.

parseInt() is just such a function. It will convert a given value into a base-ten integer.

parseInt("40") //gives us 40.

As in the above example, the second optional ‘radix‘ parameter is often missed when calling the parseInt function. The need for this parameter becomes apparent when you consider numeric constants are interpreted as octal if they are preceded by a zero, and hexadecimal if they are preceded by a zero and x (0x).

The latter case does not often present us with a problem, however consider some of the following kinds of numbers that you may use in the course of your day:

  • Bank Account
  • Sort Code
  • Insurance Policy Reference
  • Dates/Times
  • Reference Number

It is fairly commonplace for these numbers to be zero-padded. Your bank account number might be 123456, but whenever you quote it, you will use the leading zero you were provided when you established the account. So when we pass this to the function it assumes it is an octal number.

alert(parseInt("0123456")); //alerts 42798

But not always! If the bank account had any 8s or 9s in it then the above would have worked. This would have been an intermittent bug to hinder your fault investigation yet further!

So, to alleviate any issues we add a second radix parameter, which tells the compiler the base of the number being passed. In every real-world use I have required of parseInt this number has been 10;

alert(parseInt("0123456",10)); //alerts 123456

Hexadecimal numbers utilise the six alpha characters A-F, so 16 as a radix parameter will suffice for hexadecimal parameters.However, there are 26 letters in the English alphabet, aren’t there. So, care to take a stab at when the following will give?

alert(parseInt("Z",36));

For the lazy but curious among you, the answer is 35.

So, there we have it. Every reason to include the radix parameter on your parseInt, and every justification for JSLint to flag it!

For more on numbers in JavaScript, refer to The Complete JavaScript Number Reference.

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.

, , , , ,
Trackback

3 comments untill now

  1. [...] statement. Missing 'new' prefix when invoking a constructor. Missing property name. Missing quote. Missing radix parameter. Missing semicolon. Missing space after '{a}'. Nested comment. 'new' should not be used as a [...]

  2. There is one small problem with your snippets. You’re sending already parsed number to parseInt(). It should be string, not number. What currently is happening is 0123456 being converted to octal and converted value being sent to parseInt(). parseInt() expects string, so it converts number to string. With no value or value equal to 10, it doesn’t make sense (unless you want to convert float to number (in this case, 2.5 equals “2.5″ after converting to string and parseInt() ignores everything after invalid character only getting 2) or something), as number is returned again as number.

  3. Thanks. Have updated accoringly

Add your comment now