Linter · Rules
radix
Require the radix argument of parseInt()
Without a radix, parseInt picks the base from the string, so parseInt("0x10") is 16, while parseInt("0x10", 10) is 0. With input from a user or a file, that's rarely what you want. The second argument says which base you mean.
The rule checks parseInt and Number.parseInt. It also reports a call without arguments, and a radix that can't work: undefined, or a literal that isn't a whole number from 2 to 36.
For a missing radix, the report comes with a suggestion that adds 10.
Configure
Off by default. To turn it on:
[lint.rules]
"radix" = "error" # or "warn"
Every option, set to its default:
[lint.rules]
"radix" = ["error", "always"] # "always" or "as-needed"
Examples
Reported:
const page = parseInt(query.page);
const size = Number.parseInt(query.size);
const level = parseInt(input, 1);
const depth = parseInt(input, "10");
Not reported:
const page = parseInt(query.page, 10);
const color = Number.parseInt(hex, 16);
const value = parseInt(input, base);
Options
| Option | What it does |
|---|---|
"always" (default) | Require the radix. |
"as-needed" | Has no effect, as in ESLint 10. |
const page = parseInt(query.page);
If your project has an ESLint config and ESLint 9 or older installed, "as-needed" works like it does there. You can leave the radix out, and a radix of 10 is reported.