prefer-modern-math-apis

March 27, 2026 ยท View on GitHub

๐Ÿ“ Prefer modern Math APIs over legacy patterns.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Math additions in ES2015:

Currently, we only check a few known cases, but we are open to add more patterns.

If you find a suitable case for this rule, please open an issue.

Examples

// โŒ
Math.log(x) * Math.LOG10E

// โŒ
Math.LOG10E * Math.log(x)

// โŒ
Math.log(x) / Math.LN10

// โœ…
Math.log10(x)
// โŒ
Math.log(x) * Math.LOG2E

// โŒ
Math.LOG2E * Math.log(x)

// โŒ
Math.log(x) / Math.LN2

// โœ…
Math.log2(x)
// โŒ
Math.sqrt(a * a + b * b)

// โŒ
Math.sqrt(a ** 2 + b ** 2)

// โœ…
Math.hypot(a, b)
// โŒ
Math.sqrt(x ** 2)

// โœ…
Math.abs(x)

Separate rule for Math.trunc()

See unicorn/prefer-math-trunc rule.