basic
Math.cbrt
Returns the cube root of a number. Introduced in ES2015, and unlike sqrt it handles negative inputs correctly.
Math.cbrt(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | Any number; negatives are supported |
| negative input | Math.cbrt(-8) === -2, unlike Math.pow(-8, 1/3) which returns NaN |
| ES2015+ | Not in older browsers; polyfill via a signed pow trick if needed |
Examples
console.log(Math.cbrt(27)); // 3 3 cubed is 27.
console.log(Math.cbrt(-8)); // -2 Handles negatives — a big win over Math.pow(-8, 1/3).
console.log(Math.cbrt(0)); // 0 Zero returns zero.
console.log(Math.cbrt(2)); // 1.2599210498948732 Irrational cube root of 2.
Gotcha
Math.pow(-8, 1/3) is NaN because 1/3 is not exactly representable; always use Math.cbrt for signed cube roots.
Related
Math.sqrt
Returns the positive square root of a number. Equivalent to Math.pow(x, 0.5) but faster and clearer.
Math.pow
Returns base raised to the power of exponent. In modern code the `**` operator (ES2016) is usually preferred for readability and identical semantics.
Math.hypot
Returns the square root of the sum of squares of its arguments — the Euclidean norm. Introduced in ES2015 and safer than manual sqrt(a*a + b*b) against overflow.
Math.abs
Returns the absolute value of a number — its distance from zero without regard to sign. Useful for differences, magnitudes, and tolerance checks.