exponentiationOperators
Prefers the ** operator over Math.pow().
✅ This rule is included in the ts stylistic presets.
The exponentiation operator ** was introduced in ES2016 as a more readable alternative to Math.pow().
This rule suggests using ** instead of Math.pow() for exponentiation operations.
Benefits of using ** include:
- A more concise and readable syntax
- It works with BigInt values, unlike
Math.pow() - Consistency with other mathematical operators
Examples
Section titled “Examples”const result = Math.pow(2, 8);const squared = Math.pow(x, 2);const cubed = Math.pow(base, exponent);const result = 2 ** 8;const squared = x ** 2;const cubed = base ** exponent;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 environments that don’t have the exponentiation operator (pre-ES2016), you may disable this rule.
However, most modern JavaScript environments support **, and transpilers can convert it for older targets.
Some projects also prefer to use Math.pow() for consistency with older codebases or Math-oriented areas of code.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useExponentiationOperator - ESLint:
prefer-exponentiation-operator - Oxlint:
eslint/prefer-exponentiation-operator
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.