Computer >> Computer tutorials >  >> Programming >> Javascript

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


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.

<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.

 

<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