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

What is the use of Math.pow() method in JavaScript?


Math.pow()

Math.pow() function in JavaScript is used to get power of a number i.e, the value of number raised to some exponent. This method accepts two parameters one is base and the other is exponent. It doesn't matter what kind of parameters do we use such as integers, fractions, negatives etc., it  can take any value and proceeds the operation.

syntax

Math.pow(base, exponent)

Example-1

In the following example, we are taken two positive values .The first parameter(base) is 9 and second parameter(exponent) is 3. Using Math.pow() we are going to find 9 to the power of 3.

<html>
<body>
<script>
   document.write(Math.pow(9, 3));
</script>
</body>
</html>

Output

729

Example-2

Using this method we can also find square roots, cube roots etc. Following example is used to demonstrate how to find cube root of a provided number.

<html>
<body>
<script>
   document.write(Math.pow(27, 1/3));
</script>
</body>
</html>

Output

3

Example-3

Following code demonstrates how to find power of a negative number. 

<html>
<body>
<script>
   document.write(Math.pow(-9, 3));
</script>
</body>
</html>

Output

-729