unicorn/prefer-math-abs
eslint-plugin-unicorn
Suggestion
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-abs": "error",
},
},
];Prefer Math.abs() over manual absolute value expressions and symmetric range checks.
const absolute = value < 0 ? -value : value;const absolute = value <= 0 ? 0 - value : value;const absolute = value > 0 ? value : -value;const absolute = Math.abs(value);if (Math.abs(number) > MAXIMUM) {
}Examples from the ESLint documentation.
unicorn/prefer-math-abs
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-abs": "error",
},
},
];Oxlint does not have this rule.
nursery/useMathAbs
nursery · since 2.6.3
{
"linter": {
"rules": {
"nursery": {
"useMathAbs": "error"
}
}
}
}Prefer flat Math.min() and Math.max() calls over nested calls.
Prefer Math constants over their approximate numeric values.
Prefer directly returning boolean expressions over if statements.
Prefer the unary minus operator over multiplying or dividing by -1.
Prefer block statements over IIFEs used only for scoping.
Prefer putting the condition in the while statement.