prefer-numeric-literals
eslint
Safe autofix
eslint.config.js
export default [
{
rules: {
"prefer-numeric-literals": "error",
},
},
];Disallow parseInt() and Number.parseInt() in favor of binary, octal, and hexadecimal literals
parseInt("111110111", 2) === 503;
parseInt(`111110111`, 2) === 503;
parseInt("767", 8) === 503;
parseInt("1F7", 16) === 503;
Number.parseInt("111110111", 2) === 503;
Number.parseInt("767", 8) === 503;
Number.parseInt("1F7", 16) === 503;parseInt(1);
parseInt(1, 3);
Number.parseInt(1);
Number.parseInt(1, 3);
0b111110111 === 503;
0o767 === 503;
0x1F7 === 503;
a[parseInt](1,2);
parseInt(foo);
parseInt(foo, 2);
Number.parseInt(foo);
Number.parseInt(foo, 2);Examples from the ESLint documentation.
prefer-numeric-literals
eslint
export default [
{
rules: {
"prefer-numeric-literals": "error",
},
},
];prefer-numeric-literals
eslint plugin · style
{
"rules": {
"prefer-numeric-literals": "error"
}
}complexity/useNumericLiterals
complexity · since 1.0.0
{
"linter": {
"rules": {
"complexity": {
"useNumericLiterals": "error"
}
}
}
}Disallow returning value from constructor
Disallow variable or function declarations in nested blocks
Require template literals instead of string concatenation
Disallow the use of Math.pow in favor of the ** operator
Disallow unnecessary calls to .call() and .apply()
Enforce the use of the radix argument when using parseInt()