unicorn/prefer-math-constants
eslint-plugin-unicorn
Suggestion
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-constants": "error",
},
},
];Prefer Math constants over their approximate numeric values.
const area = 3.14 * radius ** 2;const growth = amount * 2.718;const diagonal = side * 1.4142;const area = Math.PI * radius ** 2;const growth = amount * Math.E;const diagonal = side * Math.SQRT2;Examples from the ESLint documentation.
unicorn/prefer-math-constants
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-constants": "error",
},
},
];Oxlint does not have this rule.
suspicious/noApproximativeNumericConstant
suspicious · since 1.3.0
{
"linter": {
"rules": {
"suspicious": {
"noApproximativeNumericConstant": "error"
}
}
}
}Prefer Math.abs() over manual absolute value expressions and symmetric range checks.
Prefer the unary minus operator over multiplying or dividing by -1.
Prefer flat Math.min() and Math.max() calls over nested calls.
Prefer putting the condition in the while statement.
Prefer directly returning boolean expressions over if statements.
Disallow confusing uses of Array#{splice,toSpliced}().