Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Math cbrt() Method

  • Last Updated : 08 Jul, 2020

Below is the example of the Math cbrt() Method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example:




    <script type="text/javascript"
       document.write("cbrt of 64 : " 
                         + Math.cbrt(64) + "<br>");
       document.write("cbrt of 27: " 
                         + Math.cbrt(27) + "<br>"); 
       document.write("cbrt of 0: " 
                         + Math.cbrt(0) + "<br>"); 
       document.write("cbrt of -1: " 
                         + Math.cbrt(-1) + "<br>"); 
       document.write("cbrt of -27: " 
                         + Math.cbrt(-27) + "<br>"); 
       document.write("cbrt of Infinity : " 
                         + Math.cbrt(Infinity)); 
    </script> 
  • Output:
    cbrt of 16: 4
    cbrt of 27: 3
    cbrt of 0: 0
    cbrt of -1: -1
    cbrt of -27: -3
    cbrt of Infinity : Infinity

The Math.cbrt() method is used to find cube root of a number.
Syntax:

Math.cbrt(x)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • x: This parameter is a simply a number whose cube root need to be find.

Returns: It returns the cube root of the given number.

Below example illustrate the cbrt() method in JavaScript:
Example: 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.
∛8
= ∛2*2*2
= 2



Input: Math.cbrt(8)
Output : 2

More codes for the above method are as follows:

Program 1: 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("cbrt of Complex no : " 
                   + Math.cbrt(1 + 2i));
</script> 

Output:

Error: Invalid or unexpected token

Program 2: 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("cbrt of a string : " 
                     + Math.cbrt("gfg"));
</script> 

Output:

cbrt of a string: NaN

Program 3: 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:

  • Google Chrome 38.0
  • Internet Explorer 12.0
  • Firefox 25.0
  • Opera 25.0
  • Safari 8.0



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!