JavaScript Reference ยท Math
Math.cbrt()
Reference for Math.cbrt(): computing cube roots of positive and negative numbers, how it differs from ** (1/3), exact results and precision notes.
Math.cbrt() returns the cube root of a number: the value that, multiplied by itself three times, gives the argument. Math.cbrt(27) is 3 and Math.cbrt(-8) is -2.
Unlike the square root, a cube root exists for negative numbers, so Math.cbrt() returns a real result for any finite input. That is its main advantage over x ** (1 / 3), which gives NaN for a negative base.
Math.cbrt(x)
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
Math.cbrt(x)
| Name | Description |
|---|---|
x |
A number (or a value that converts to one). |
Return value: the cube root of x. The sign of x is preserved, -0 stays -0, and Infinity and -Infinity map to themselves. Non-numeric input gives NaN.
Exceptions: a BigInt argument throws a TypeError.
Usage notes
- Works with negatives.
Math.cbrt(-64)is-4. The expression(-64) ** (1 / 3)isNaN. - Exact for perfect cubes. Engines return exact answers for the common cases such as
8,27and1000, which a floating-point** (1 / 3)often misses because1 / 3is not exactly one third. - Irrational results like
Math.cbrt(2)are, as always, approximations. Compare with a tolerance and round for display. - Perfect-cube test. Use
Number.isInteger(Math.cbrt(n))for modest integers; round the root and cube it again to be safer with larger values. - Other roots. For a fourth root and higher, use
x ** (1 / n), remembering that negative bases giveNaN. For odd roots of negative numbers, take the root of the absolute value and restore the sign yourself. - Related geometry: the diameter of a sphere from its volume needs a cube root, as does scaling 3D models uniformly.
Browser support
Part of ES2015 and supported in all current browsers.