JavaScript Math.cbrt() Method
-
Syntax of JavaScript
Math.cbrt()
Method -
Example 1: Use the
Math.cbrt()
Method to Get Cubic Root of a Number -
Example 2: Use
Math.cbrt()
to Get and Compare Cubic Root of Numbers -
Example 3: Use
Math.cbrt()
on aNEGATIVE_INFINITY
Value -
Example 4: Use
Math.cbrt()
on anull
Value
The Math.cbrt()
method takes any number to find its cubic root. In JavaScript, cbrt
is a static method and can only be used for Math
objects.
Syntax of JavaScript Math.cbrt()
Method
Math.cbrt(x);
Parameters
x |
A number is required to find its cubic root. |
Return
This method calculates and returns the cubic root of a number.
Example 1: Use the Math.cbrt()
Method to Get Cubic Root of a Number
We use the method Math.cbrt()
to get the cubic root of a number in JavaScript. In the example below, we got 125
by multiplying 5*5*5
.
Then, we used the Math.cbrt()
method to check that 5 is the cubic root of the number 125.
let cbrt = 5;
let num = cbrt*cbrt*cbrt;
console.log(num);
console.log(Math.cbrt(125));
Output:
125
5
Example 2: Use Math.cbrt()
to Get and Compare Cubic Root of Numbers
We get the cubic root of negative and positive numbers using the Math.cbrt()
method. In this example, we have used the cbrt()
method to get the cubic root of -8 and 8.
let cbrt = Math.cbrt(-8);
let cb = Math.cbrt(8);
console.log(cbrt);
console.log(cb);
Output:
-2
2
Example 3: Use Math.cbrt()
on a NEGATIVE_INFINITY
Value
When we pass the NEGATIVE_INFINITY
as a parameter of the Math.cbrt()
method, it always returns the -Infinity
value that users can observe in the output of the example below.
let value = -Infinity;
let cbrt = Math.cbrt(value);
console.log(cbrt);
Output:
-Infinity
Example 4: Use Math.cbrt()
on a null
Value
In the example below, we have tried to use different values as a parameter of the Math.cbrt()
method.
Users can see that Math.cbrt()
returns 0 for the empty string ""
and null
values. The method returns NaN
for the undefined
variables, which represent Not a Number
in JavaScript.
let val1;
let val2 = null;
let cbrt = Math.cbrt(val1);
let cb = Math.cbrt(val2);
console.log(cbrt);
console.log(cb);
console.log(Math.cbrt(undefined));
console.log(Math.cbrt(""));
Output:
NaN
0
NaN
0
The Math.cbrt()
method is supported in all browsers of the current version.