660

I ran JSLint on this JavaScript code and it said:

Problem at line 32 character 30: Missing radix parameter.

This is the code in question:

imageIndex = parseInt(id.substring(id.length - 1))-1;

What is wrong here?

0

11 Answers 11

1150

It always a good practice to pass radix with parseInt -

parseInt(string, radix)

For decimal -

parseInt(id.substring(id.length - 1), 10)

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)

(Reference)

16
  • 7
    From the sounds of it, the default IS 10. If it doesn't begin with 0x or 0, it defaults to a radix of 10. But it is best practice to specify a radix even if it is the default value, sort of like specifying the definition of "this" to an array.map function.
    – molson504x
    Dec 15, 2015 at 13:07
  • 113
    thats so unreasonable... by that logic there should be a third param to represent the radix of the radix argument itself
    – Nishant
    May 30, 2016 at 4:46
  • 8
    Agree with other commenters. Why is it good to provide a radix value when the default is 10? This defies common convention. Jul 10, 2016 at 6:13
  • 15
    Add 10 as the radix to get another lint error... Redundant radix parameter
    – Shanimal
    Jan 24, 2018 at 7:28
  • 3
    @Nishant: The radix argument is a numeric value, not a string representation of a numeric value, so there is no radix to specify.
    – tokland
    Feb 11, 2018 at 21:16
157

To avoid this warning, instead of using:

parseInt("999", 10);

You may replace it by:

Number("999");


Note that parseInt and Number have different behaviors, but in some cases, one can replace the other.

3
  • 8
    There are also big performance differences between parseInt and Number. Here is an old performance test.
    – Josh Unger
    Jan 18, 2019 at 0:19
  • 12
    Chrome 77: Number() is 6x faster than parseInt()
    – Zanon
    Oct 30, 2019 at 16:07
  • 1
    The real answer. Aug 8, 2022 at 10:53
53

I'm not properly answering the question but, I think it makes sense to clear why we should specify the radix.

On MDN documentation we can read that:

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  • [...]
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  • [...]

Source: MDN parseInt()

4
  • 2
    Yes but the Typescript compiler will insert it, so why should you bother?
    – Spock
    Aug 30, 2016 at 20:51
  • 2
    @Spock Because TSLint complains that it's not there. And down the rabbit hole we go...
    – msanford
    Nov 1, 2016 at 15:11
  • 1
    Yeah true.. that's why I just disable this lint rule. Still don't understand why an OPTIONAL parameter trips a lint complaint.. oh well
    – Spock
    Nov 1, 2016 at 18:14
  • 4
    @Spock Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10. Reference
    – Andrew Lam
    Jun 3, 2017 at 5:10
34

You can turn off this rule if you wish to skip that test.

Insert:

radix: false

Under the "rules" property in the tslint.json file.

It's not recommended to do that if you don't understand this exception.

1
  • 1
    I'm going to use this since code runs just fine without radix
    – William
    Jul 4, 2018 at 11:23
22

Adding the following on top of your JS file will tell JSHint to supress the radix warning:

/*jshint -W065 */

See also: http://jshint.com/docs/#options

5
  • 2
    What jshint option does this correspond to? I'm using SublimeLint to run jshint in my editor, and it only takes a hash of option: value pairs for it's setting, so I don't think I can apply your "-W065" suggestion.
    – Dihedral
    Jul 9, 2013 at 19:12
  • 5
    You can use "-W065": true, e.g. in a .jshintrc file. Sep 25, 2013 at 11:50
  • 30
    -1 Please don't do this, just add the radix you want to parse in Aug 13, 2014 at 18:03
  • The more strongly-typed a language, the more opportunities for compiler optimization, which is why it is throwing the warn. Jul 25, 2017 at 13:27
  • 3
    in modern JS, IMO adding the radix actually makes it more unclear what the function is doing. It's in the position you might expect a default to go if you don't know the function signature. It makes no sense that you have to specify a radix. Mar 4, 2018 at 18:30
19

Prior to ECMAScript 5, parseInt() also autodetected octal literals, which caused problems because many developers assumed a leading 0 would be ignored.

So Instead of :

var num = parseInt("071");      // 57

Do this:

var num = parseInt("071", 10);  // 71

var num = parseInt("071", 8);

var num = parseFloat(someValue); 

Reference

5

Simply add your custom rule in .eslintrc which looks like that "radix": "off" and you will be free of this eslint unnesesery warning. This is for the eslint linter.

4

I solved it with just using the +foo, to convert the string.

Keep in mind it's not great for readability (dirty fix).

console.log( +'1' )
// 1 (int)
4

You can also simply add this line right above your parseInt line:

// eslint-disable-next-line

This will disable eslint check for the next line. Use this if you only need to skip one or two lines.

2

Just put an empty string in the radix place, because parseInt() take two arguments:

parseInt(string, radix);

string The value to parse. If the string argument is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string argument is ignored.

radix An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above-mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

imageIndex = parseInt(id.substring(id.length - 1))-1;
imageIndex = parseInt(id.substring(id.length - 1), '')-1;

0

Instead of calling the substring function you could use .slice()

    imageIndex = parseInt(id.slice(-1)) - 1;

Here, -1 in slice indicates that to start slice from the last index.

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.