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

JavascriptObject Oriented ProgrammingFront End Technology

Using Math object in javascript any kind of operation can be done. Math.cbrt() is a method especially used to find the cube root of a number. It takes a number as a parameter and returns its cube root.

syntax

Math.cbrt(64);

It takes a number as a parameter and returns its cube root value as the output.

Example-1

In the following example, the cube roots of only positive values were found out and displayed in the output.

Live Demo

<html>
<body>
<script>
   document.write(Math.cbrt(64));
   document.write("</br>");
   document.write(Math.cbrt(27));
   document.write("</br>");
   document.write(Math.cbrt(0));
</script>
</body>
</html>

Output

4
3
0

Example-2

This method can take negative values also. In the following example, the cube roots of the negative values were found out and displayed in the output.

 Live Demo

<html>
<body>
<script>
   document.write(Math.cbrt(-64));
   document.write("</br>");
   document.write(Math.cbrt(-27));
   document.write("</br>");
   document.write(Math.cbrt(0));
   document.write("</br>");
   document.write(Math.cbrt(Infinity));
</script>
</body>
</html>

Output

-4
-3
0
Infinity


raja
Published on 31-Jul-2019 16:10:33
Advertisements