602

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?

Rimian
  • 32,654
  • 13
  • 106
  • 109
Mike Vierwind
  • 6,189
  • 3
  • 13
  • 9

11 Answers11

1072

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)

Community
  • 1
  • 1
Jayendra
  • 50,361
  • 4
  • 75
  • 89
  • 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 '15 at 13:07
  • 99
    thats so unreasonable... by that logic there should be a third param to represent the radix of the radix argument itself – Nishant May 30 '16 at 04:46
  • 6
    Agree with other commenters. Why is it good to provide a radix value when the default is 10? This defies common convention. – Richard Clayton Jul 10 '16 at 06:13
  • Can somebody like @vbullinger mentioned say this is default 10... for typescript this error was annoying to solve. – Christian Matthew May 25 '17 at 16:07
  • @Jayendra Thank you for your explanation. Could you also further elaborate on how to define the radix? I see this warning message when I am using radix, _**'radix' was used before it was defined.**_. – Andrew Lam Jun 03 '17 at 04:36
  • 14
    Add 10 as the radix to get another lint error... `Redundant radix parameter` – Shanimal Jan 24 '18 at 07:28
  • 2
    @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 '18 at 21:16
  • @vbullinger and others, the radix is there so that you can clearly see, **what are you parsing to**. And that it is exactly the output, you are expeting from `parseInt` to provide. Look on the internet, or just here, how many questions are about `parseInt`s output being wrong, when radix wasn't provided. – Pavel Hasala Apr 16 '18 at 16:52
  • Sure thing, @PavelHasala. But... let's default it to ten. Thanks. – vbullinger Apr 16 '18 at 21:52
  • 1
    The default value of `radix` as a function argument is conditional, not `10`. The default of those conditions is `10`, but that's not the same thing. `parseInt('0xA') !== parseInt('0xA', 10)` ...it is unexpected. – tybro0103 Jul 16 '18 at 22:26
  • Please note that you can also use the as-needed es-lint option so that you only need to specify a radix if the radix != 10 https://eslint.org/docs/rules/radix#options – Nico Timmerman Apr 04 '19 at 11:15
  • 1
    @Nishant Why not just use `Number` if that's what you want? – Emobe May 12 '19 at 15:51
  • @Emobe That would be perfect. Just read my comment after a long time. Realize I was indeed looking at it the wrong way like @@tokland said. Strange, my comment was upvoted 56 times – Nishant May 13 '19 at 12:48
  • 1
    @Nishant well, your comment does make sense, it's pretty unreasonable to mark a parameter as required when it has a default value. However I think the intention of the tslint rule is to try and sway people to use appropriate methods. While parseInt _can_ create a base 10 number, it's intended for different bases too. Another downside to `parseInt` is that it's a performance hog. https://jsperf.com/number-vs-parseint-vs-plus/3 So if you're not wanting anything other than base 10, then `Number` would be the way to go imo. – Emobe May 13 '19 at 13:26
107

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.

Zanon
  • 22,850
  • 18
  • 101
  • 110
  • 7
    There are also big performance differences between `parseInt` and `Number`. Here is an [old performance test](https://jsperf.com/number-vs-parseint-vs-plus/3). – Josh Unger Jan 18 '19 at 00:19
  • 8
    Chrome 77: `Number()` is 6x faster than `parseInt()` – Zanon Oct 30 '19 at 16:07
44

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()

nmoliveira
  • 1,609
  • 15
  • 14
  • 1
    Yes but the Typescript compiler will insert it, so why should you bother? – Spock Aug 30 '16 at 20:51
  • 2
    @Spock Because TSLint complains that it's not there. And down the rabbit hole we go... – msanford Nov 01 '16 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 01 '16 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) – Andrew Lam Jun 03 '17 at 05:10
31

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.

Spock
  • 2,139
  • 26
  • 26
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

aleemb
  • 28,346
  • 17
  • 92
  • 111
  • 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 09 '13 at 19:12
  • 5
    You can use `"-W065": true`, e.g. in a `.jshintrc` file. – alexfernandez Sep 25 '13 at 11:50
  • 29
    -1 Please don't do this, just add the radix you want to parse in – Juan Mendes Aug 13 '14 at 18:03
  • The more strongly-typed a language, the more opportunities for compiler optimization, which is why it is throwing the warn. – HoldOffHunger Jul 25 '17 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. – Charles Offenbacher Mar 04 '18 at 18:30
5

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)
user2369834
  • 55
  • 1
  • 3
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.

Rohit Nethi
  • 119
  • 5
3

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.

Goran_Ilic_Ilke
  • 343
  • 2
  • 11
3

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

geisterfurz007
  • 3,390
  • 5
  • 29
  • 46
SanTom
  • 31
  • 1
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;

Ahmed.Dz
  • 21
  • 4
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.

Daniel Selvan
  • 611
  • 5
  • 18