How to find the cube root of a number in JavaScript?



In this article we are going to discuss how find the cube root of a number with the help of suitable examples in JavaScript.

Using Math object in JavaScript any kind of operation can be done. Math.cbrt(), Math.pow() are the methods especially used to find the cube root of a number and also we can achieve it by using ** operator. It takes a number as a parameter and returns its cube root.

To find the cube root of a number in JavaScript, there are three possible ways. The way to do it is by using cbrt() method, second way is by using pow() method and the third possible way to do it is by using ** operator in JavaScript.

Syntax

The syntax for the cbrt() method is shown below.

Math.cbrt(value)

Here, value is taken as input and it should be a number. It returns cube root of the value.

Example 1

The following is an example program to find the cube root of a number using cbrt() method.

<!DOCTYPE html>
<html>
<head>
   <title>To find the cube root of a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>To find the cube root of a number in JavaScript</h3>
   <p id='cbrt'></p>
   <script>
      let a = Math.cbrt(36, 1/3);
      let b = Math.cbrt(125,1/3);
      let c = Math.cbrt(-36.1/3);
      let d = Math.cbrt(-125,1/3);
      document.getElementById('cbrt').innerHTML = `The cube root value for 36 is ${a} ${'<br/>'} The cube root value for 125 is ${b} ${'<br/>'} The cube root value for -36 is ${c} ${'<br/>'} The cube root value for -125 is ${d} ${'<br/>'}`;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 2

The following is an example program to find the cube root of a number using pow() method.

<!DOCTYPE html>
<html>
<head>
   <title>To find the cube root of a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>To find the cube root of a number in JavaScript</h3>
   <p id='cbrt'></p>
   <script>
      let a = Math.pow(27,1/3);
      let b = Math.pow(125,1/3);
      let c = Math.pow(64,1/3);
      let d = Math.pow(39,1/3);
      document.getElementById('cbrt').innerHTML = `The cube root value for 27 is ${a} ${'<br/>'} The cube root value for 125 is ${b} ${'<br/>'} The cube root value for 64 is ${c} ${'<br/>'} The cube root value for 39 is ${d} ${'<br/>'}`;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 3

The following is an example program to find the cube root of a number using ** operator.

<!DOCTYPE html>
<html>
<head>
   <title>To find the cube root of a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>To find the cube root of a number in JavaScript</h3>
   <p id='cbrt'></p>
   <script>
      let a = 27 ** (1/3);
      let b = 125 ** (1/3);
      let c = 36 ** (1/3);
      let d = 75 ** (1/3);
      document.getElementById('cbrt').innerHTML = `The cube root value for 27 is ${a} ${'<br/>'} The cube root value for 125 is ${b} ${'<br/>'} The cube root value for 36 is ${c} ${'<br/>'} The cube root value for 75 is ${d} ${'<br/>'}`;
   </script>
</body>
</html>

On executing the above code, the following output is generated.


Advertisements