The cbrt()
method computes the cube root of a specified number and returns it.
Example
// cube root of 8
let number = Math.cbrt(8);
console.log(number);
// Output: 2
cbrt() Syntax
The syntax of the Math.cbrt()
method is:
Math.cbrt(number)
Here, cbrt()
is a static method. Hence, we are accessing the method using the class name, Math
.
cbrt() Parameter
The cbrt()
method takes a single parameter:
number
- value whose cube root is to be calculated
cbrt() Return Value
The cbrt()
method returns:
- the cube root of a given
number
- NaN (Not a Number) if the argument is non-numeric
Example 1: JavaScript Math.cbrt()
// cbrt() with an integer number
let number1 = Math.cbrt(27);
console.log(number1);
// cbrt() with a decimal number
let number2 = Math.cbrt(64.144);
console.log(number2);
// Output:
// 3
// 4.002997752808288
Here, we have used the Math.cbrt()
method to compute the cube root of an integer value, 27
and a decimal value, 64.144
.
Example 2: cbrt() with Numeric String
// cbrt() with a numeric string
let number2 = Math.cbrt("125");
console.log(number2);
// Output: 5
In the above example, the Math.cbrt()
method converts the numeric string "125"
into a number and then computes its cube root.
Example 3: Cube Root of String Argument
let string = "Harry";
// cbrt() with string as argument
let number = Math.cbrt(string);
console.log(number);
// Output: NaN
In the above example, we have tried to calculate the cube root of the string "Harry"
. That's why we get NaN as the output.
Recommended readings: