JavaScript Math.cbrt() Method



In general, the cube root of a number is a number which is multiplied by 3 times to get thr original number. In other words, the cube root of a number (x) is a value (y) such that yyy = x.

The JavaScript Math.cbrt() method accepts a number as a parameter and calculates the cuberoot of a provided number. If we pass an empty number or non-numeric value as an argument to this method, it returns "NaN" as result.

Syntax

Following is the syntax of JavaScript Math.cbrt() method −

Math.cbrt(x);

Parameters

This method accepts only one parameter. The same is described below −

  • x: A numeric value.

Return value

This method returns the cube root of the provided number.

Example 1

In the following example, we are using the JavaScript Math.cbrt() method to calculate the cube root of the provide number −

<html>
<body>
<script>
   let number = Math.cbrt(216);
   document.write(number);
</script>
</body>
</html>

Output

The above program returns 6 as result.

Example 2

Here, we are calculating the cube root of 0 and 1 −

<html>
<body>
<script>
   let number1 = Math.cbrt(0);
   let number2 = Math.cbrt(1);

   document.write(number1, "<br>", number2);
</script>
</body>
</html>

Output

It returns 0 and 1 as cuberoots.

Example 3

In this example, we are passing a numeric value as a string to this method. The cbrt() method converts the numeric string into a number and then calculates its cube root −

<html>
<body>
<script>
   let number = Math.cbrt("20");
   document.write(number);
</script>
</body>
</html>

Output

The above program returns "2.7144176165949063" as cuberoot.

Example 4

If we pass an empty number or non-numeric value as an argument to this method, it returns "NaN" as output −

<html>
<body>
<script>
   let number1 = Math.cbrt("Tutorialspoint");
   let number2 = Math.cbrt();

   document.write(number1, "<br>", number2);
</script>
</body>
</html>

Output

If we execute the above program, it returns "NaN" as result.

Advertisements