The Math.cbrt() function is an inbuilt function in JavaScript which is used to find cube root of a number.
Syntax:
Math.cbrt(x)
- Here x is simply a number whose cube root need to find.
- It returns the cube root of the given number.
Parameters:
Return value:
Example:
Input: Math.cbrt(8)
Output: 2
Explanation:
∛8
= ∛2*2*2
= 2
Here cube root of 8 is calculated to 2 because when any 3 times repeated any number is present
inside of the cube root then only one number is taken out as the value of the cube root.
Input: Math.cbrt(64)
Output: 4
Explanation:
∛64
= ∛4*4*4
= 4
Let’s see JavaScript program:
// Here the Math.cbrt() function calculates cube root for // different numbers taken as function's parameter. console.log(Math.cbrt(64)); console.log(Math.cbrt(27)); console.log(Math.cbrt(0)); console.log(Math.cbrt(-1)); console.log(Math.cbrt(-27)); console.log(Math.cbrt(Infinity)); |
Output:
> 4 > 3 > 0 > -1 > -3 > Infinity
- It is an error case because cube root of complex number can not be found that is why its parameter gives error.
// cube root of complex number can not be calculated.
console.log(Math.cbrt(1 + 2i));
Output:
Error: Invalid or unexpected token
- Cube root of string can not be found that is why string parameter of the function gives NaN i.e, not a number.Only integer value can be used as the parameter for the function.
// Only number can be used as the parameter
// here string as parameter gives NaN i.e, not a number.
console.log(Math.cbrt(
"gfg"
));
Output:
> NaN
Errors and Exceptions:
- Whenever we need to get cube root of any number that time we take the help of the Math.cbrt() function in JavaScript.
Let’s see JavaScript program:// Here the Math.cbrt() function calculates cube root for
// different numbers taken as function's parameter.
console.log(Math.cbrt(125));
console.log(Math.cbrt(23));
Output:
> 5 > 2.8438669798515654
Application:
leave a comment
0 Comments