Math.cbrt()

Returns the cube root of a number, handling negative input correctly.

Signature
Math.cbrt(x)
  • Added in JavaScript ES2015

Parameters

ParameterTypeDescription
$xnumberA number, positive or negative. Non-numbers are coerced first.

Return value

numberThe cube root of x — the unique y such that y * y * y === x. Sign is preserved, so -0 returns -0 and -Infinity returns -Infinity.

Math.cbrt(x) returns the real cube root of x. Unlike Math.sqrt(), it accepts negative input and returns a negative result: Math.cbrt(-8) is -8's real cube root, -2.

When to reach for it

The obvious case is inverting a cube: recovering a side length from a volume, or a radius from a sphere's volume. The less obvious and more common one is perceptual scaling — cube-root scaling is used in colour work (the CIE Lab lightness transfer function is a cube root) and to size symbols on maps by volume rather than area.

What trips people up

The reason this method exists is that the intuitive alternative is broken. Math.pow(-8, 1 / 3) returns NaN, for two compounding reasons: a negative base with a non-integer exponent is defined as NaN by the spec, and 1 / 3 is not exactly one third in binary anyway. Always use Math.cbrt() for cube roots, never a fractional exponent.

Results are approximate, as with any floating-point root. Math.cbrt(64) is exactly 4, but many values are not exact — do not compare cubed results with ===.

Math.cbrt() is ES2015. In pre-ES6 environments it needs a polyfill built on Math.sign and Math.pow(Math.abs(x), 1/3).

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Recovering a cube's side length from its volume
  • Deriving a sphere's radius from its volume
  • Applying cube-root scaling to size map symbols by volume
  • Implementing the CIE Lab lightness transfer, which uses a cube root

Frequently asked questions

How do I get a cube root in JavaScript?

Call Math.cbrt(x). It works for negative numbers too, which Math.pow(x, 1/3) does not — that returns NaN for any negative base.

Why does Math.pow(-8, 1/3) return NaN instead of -2?

Because the spec defines a negative base with a non-integer exponent as NaN, and 1/3 is not exactly one third in binary floating point. Math.cbrt(-8) returns -2 correctly.

Is Math.cbrt() available everywhere?

It was added in ES2015 and has been available across browsers since 2015. Environments older than that need a polyfill using Math.sign(x) * Math.pow(Math.abs(x), 1/3).

More examples

Volume to dimension

Inverting V = (4/3)πr³ recovers a radius of 10 from a volume of about 4188.79. The last two lines show that exact round-trips are luck: clean cubes come back exactly, arbitrary values pick up rounding error, so compare with a tolerance.

Example · javascript
const sideFromVolume = (v) => Math.cbrt(v);
console.log(sideFromVolume(1000)); // 10

function sphereRadius(volume) {
  return Math.cbrt((3 * volume) / (4 * Math.PI));
}

const r = sphereRadius(4188.79);
console.log(r.toFixed(3)); // "10.000"

// Round-tripping is approximate, not exact
console.log(Math.cbrt(10 ** 3) === 10); // true
console.log(Math.cbrt(7) ** 3 === 7);   // false

Cube-root scaling for sizing

Linear scaling makes the largest city 1000 times the width of the smallest, which is unreadable on a map. Cube-root scaling compresses that to a factor of 10, matching how people judge relative size by volume rather than by length.

Example · javascript
const populations = [1000, 8000, 64000, 1000000];

for (const p of populations) {
  const linear = p / 1000;
  const scaled = Math.cbrt(p) / Math.cbrt(1000);
  console.log(p, "linear:", linear, "cbrt:", scaled.toFixed(2));
}

Discussion

  • Be the first to comment on this lesson.