JSLint says “missing radix parameter”; what should

2019-01-08 03:50发布

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?

8条回答
欢心
2楼-- · 2019-01-08 04:19

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-08 04:20

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.

查看更多
疯言疯语
4楼-- · 2019-01-08 04:20

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.

查看更多
Animai°情兽
5楼-- · 2019-01-08 04:23

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)
查看更多
走好不送
6楼-- · 2019-01-08 04:27

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)

查看更多
Viruses.
7楼-- · 2019-01-08 04:30

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

查看更多
登录 后发表回答