Skip to content

math/prefer-math-cbrt

enforce the use of Math.cbrt() instead of other cube root calculations

  • ⚙️ This rule is included in "plugin:math/recommended".
  • 🔧 The --fix option on the command line can automatically fix some of the problems reported by this rule.
  • 💡 Some problems reported by this rule are manually fixable by editor suggestions.

📖 Rule Details

This rule aims to enforce the use of Math.cbrt() instead of other cube root calculations.

Math.cbrt() provides several advantages over manual exponentiation:

  • Clarity of intent: Immediately obvious that you're calculating a cube root
  • Performance: Optimized native implementation for cube root calculations
  • Precision: More accurate for edge cases and special values
  • Readability: Shorter and more expressive than exponentiation syntax
  • Consistency: Part of the ES2015 Math API family
Now loading...

Important Edge Case Behavior

Note that the results are different when the input is -Infinity:

js
Math.cbrt(-Infinity)        // -Infinity
Math.pow(-Infinity, 1/3)    // Infinity
(-Infinity) ** (1/3)        // Infinity

If you do not use -Infinity in your calculations, it is a safe replacement. For most practical applications involving real numbers, Math.cbrt() provides the expected mathematical behavior.

🔧 Options

Nothing.

📚 Further reading

🚀 Version

This rule was introduced in eslint-plugin-math v0.3.0

🔍 Implementation