JavaScript | Math.cbrt() function
The Math.cbrt() function is an inbuilt function in JavaScript which is used to find cube root of a number.
Syntax:
Math.cbrt(x)
Parameters: This function accepts a sinle parameter, which is a simply a number whose cube root need to find.
Returns: It returns the cube root of the given number.
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.
Below codes will illustrate the function:
- Example 1: Multiple numbers’ cbrt.
<script type="text/javascript">document.write("Output : "+ Math.cbrt(64) +"<br>");document.write("Output : "+ Math.cbrt(27) +"<br>");document.write("Output : "+ Math.cbrt(0) +"<br>");document.write("Output : "+ Math.cbrt(-1) +"<br>");document.write("Output : "+ Math.cbrt(-27) +"<br>");document.write("Output : "+ Math.cbrt(Infinity));</script>chevron_rightfilter_noneOutput:
Output : 4 Output : 3 Output : 0 Output : -1 Output : -3 Output : Infinity
- Example 2: Errors and Exceptions, it is an error case because cube root of complex number can not be found that is why its parameter gives error.
<script type="text/javascript">// cube root of complex number can not be calculated.document.write("Output : "+ Math.cbrt(1 + 2i));</script>chevron_rightfilter_noneOutput:
Error: Invalid or unexpected token
- Example 3: Cube root of string can not be found that is why string parameter of the function gives NaN i.e, not a number. An only integer value can be used as the parameter for the function.
<script type="text/javascript">// Only number can be used as the parameter// here string as parameter gives NaN i.e, not a number.document.write("Output : "+ Math.cbrt("gfg"));</script>chevron_rightfilter_noneOutput:
Output: NaN
Application: Whenever we need to get cube root of any number that time we take the help of the Math.cbrt() function in JavaScript.
<script type="text/javascript"> // Here the Math.cbrt() function calculates cube root for // different numbers taken as function's parameter. document.write("Output : " + Math.cbrt(125) + "<br>"); document.write("Output : " + Math.cbrt(23) + "<br>"); </script> |
Output:
Output : 5 Output : 2.8438669798515654
Supported Browsers: The browsers supported by JavaScript Math.cbrt() function are listed below:
- Google Chrome 38.0
- Internet Explorer 12.0
- Firefox 25.0
- Opera 25.0
- Safari 8.0
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- JavaScript | Function Parameters
- JavaScript | Function Definitions
- JavaScript | toString( ) function
- JavaScript | Math.pow( ) Function
- JavaScript | Array every() function
- JavaScript | String() Function
- JavaScript | Function Apply
- JavaScript | Function Call
- Javascript | Number() Function
- JavaScript | Math.E() function
- JavaScript | Array some() function
- JavaScript | toFixed( ) Function
- JavaScript | Array.of() function
- JavaScript | Math.abs( ) function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



