Math.cbrt()
ES6+Returns the cube root of a number.
Syntax
Math.cbrt(x)Parameters
x number A number
Return Value
number
The cube root of the given number
Examples
JavaScript
console.log(Math.cbrt(8));
console.log(Math.cbrt(27));
console.log(Math.cbrt(-8)); Output:
// 2
3
-2
📌 When to Use
Use Math.cbrt() for volume calculations, 3D graphics scaling, or scientific formulas involving cube roots.
⚠️ Common Mistakes
Using Math.pow(x, 1/3) for negative numbers returns NaN, while Math.cbrt(-8) correctly returns -2.
Using Math.cbrt() in ES5 environments without a polyfill.
✅ Best Practices
Prefer Math.cbrt() over Math.pow(x, 1/3) for negative numbers to get correct results.
For ES5 compatibility, use: Math.sign(x) * Math.pow(Math.abs(x), 1/3).
⚡ Performance Notes
Math.cbrt() is optimized by JavaScript engines and faster than Math.pow(x, 1/3). It also handles negative numbers correctly without additional logic.
🌍 Real World Example
Cube Side Length from Volume
Calculate the side length of a cube given its volume, useful for 3D printing or packaging calculations.
function calculateCubeDimensions(volume) {
const sideLength = Math.cbrt(volume);
return {
volume,
sideLength: sideLength.toFixed(3),
surfaceArea: (6 * sideLength * sideLength).toFixed(3),
diagonal: (sideLength * Math.sqrt(3)).toFixed(3)
};
}
console.log(calculateCubeDimensions(27));
// { volume: 27, sideLength: '3.000', surfaceArea: '54.000', diagonal: '5.196' }
console.log(calculateCubeDimensions(1000));
// { volume: 1000, sideLength: '10.000', surfaceArea: '600.000', diagonal: '17.321' }