Skip to content

Prefer modern Math APIs over legacy patterns ​

💼 This rule is enabled in the ✅ recommended config.

🔧 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.

Prefer Math.log10(x) over ​

js
Math.log(x) * Math.LOG10E
js
Math.LOG10E * Math.log(x)
js
Math.log(x) / Math.LN10

Prefer Math.log2(x) over ​

js
Math.log(x) * Math.LOG2E
js
Math.LOG2E * Math.log(x)
js
Math.log(x) / Math.LN2

Prefer Math.hypot(…) over ​

js
Math.sqrt(a * a + b * b)
js
Math.sqrt(a ** 2 + b ** 2)
js
Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))

This case requires prefer-exponentiation-operator rule to fix it first.

js
Math.sqrt(x ** 2)
// This case fix to `Math.abs(x)`, since it should be better than `Math.hypot(x)`

Separate rule for Math.trunc() ​

See unicorn/prefer-math-trunc rule.

Released under the Apache License 2.0.