Skip to content

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 logarithm
  • Math.log2(): Calculates the base-2 logarithm
  • Math.hypot(): Calculates the Euclidean distance (square root of sum of squares)
  • Math.abs(): Returns the absolute value
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);

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.