mathMethods
Prefer modern Math methods over legacy patterns.
✅ This rule is included in the ts stylisticStrict presets.
ES2015 introduced several new Math methods that simplify common mathematical operations.
Using these modern methods improves code readability and intent.
This rule flags legacy patterns that can be replaced with:
Math.log10(): Calculates the base-10 logarithmMath.log2(): Calculates the base-2 logarithmMath.hypot(): Calculates the Euclidean distance (square root of sum of squares)Math.abs(): Returns the absolute value
Examples
Section titled “Examples”const log10Value = Math.log(x) * Math.LOG10E;const log10Value = Math.log(x) / Math.LN10;const log2Value = Math.log(x) * Math.LOG2E;const log2Value = Math.log(x) / Math.LN2;const absolute = Math.sqrt(x ** 2);const distance = Math.sqrt(x ** 2 + y ** 2);const log10Value = Math.log10(x);const log2Value = Math.log2(x);const absolute = Math.abs(x);const distance = Math.hypot(x, y);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you need to support JavaScript environments without ES2015 Math methods, you may need to disable this rule.
For projects with existing codebases that use these legacy patterns consistently, refactoring may not be worthwhile.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-modern-math-apis - Oxlint:
unicorn/prefer-modern-math-apis
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.