The JavaScript pow function is a Math function useful to calculate the Power of the specified expression, and its syntax is:
Math.pow(base, Exponent);
Parameters
- Base: Please specify the base value here.
- Exponent: Please specify the Exponent value or power here.
Return Value: For example, if x is the base value and 2 is the exponent, then the Math.pow(x, 2) = x².
- The pow function will return NaN if the Base or Exponent value is not a number.
- If the base is + or – 1 and the exponent value is positive or negative infinity, it returns NaN.
- When the base is less than 0, and the exponent is not an integer, it returns NaN.
- If the Base or Exponent value is Null, it will return Zero.
JavaScript pow Function Example
The pow function returns the Power of the given number. In this example, we will find the power of different data types and display the output.
First, we used the JavaScript pow Function on both the Positive and negative decimal numbers. From the screenshot below, you can see that the power function is returning NaN (Not a Number) as output for negative numbers.
Next, we used the Power Function on the string text for Str. The JavaScript statement below converts the “3” to an integer.
For Str1, we used the pow() math method on “String”. As we mentioned before, it returns NaN as output. Last, we used the Null value as the second argument. Here, null is converted to Zero.
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptPOWER Function </title>
</head>
<body>
<h1> JavaScriptPOW Function </h1>
<p id = "Pos"></p>
<p id = "Neg"></p>
<p id = "Multi"></p>
<p id = "Dec"></p>
<p id = "Neg_Dec"></p>
<p id = "Str"></p>
<p id = "Str1"></p>
<p id = "Null"></p>
<script>
document.getElementById("Pos").innerHTML = Math.pow(2, 3);
document.getElementById("Neg").innerHTML = Math.pow(-2, 3);
document.getElementById("Multi").innerHTML = Math.pow(2, -3);
document.getElementById("Dec").innerHTML = Math.pow(2.50, 4.05);
document.getElementById("Neg_Dec").innerHTML = Math.pow(-3.10,-6.05);
document.getElementById("Str").innerHTML = Math.pow(2, "3");
document.getElementById("Str1").innerHTML = Math.pow(2, "String");
document.getElementById("Null").innerHTML = Math.pow(4, null);
</script>
</body>
</html>

Example: Here, we will use the Math.pow() function with NaN arguments. If we pass the base value as NaN and the exponent as 0, it returns 1. Except for this, any combination of NaN in the method returns Not a number as the output.
console.log(Math.pow(NaN, 0));
console.log(Math.pow(NaN, 1));
console.log(Math.pow(2, NaN));
console.log(Math.pow(NaN, NaN));
1
NaN
NaN
NaN
Does JavaScript Math.pow() work with negative numbers or fractional exponents?
Yes. However, the result depends on the combination of negative and fractional exponents as base and the exponent.
Positive Base value with positive fractional exponent
If we use the pow() function with a positive base value and a positive fractional exponent, it works normally and returns the result.
console.log(Math.pow(5, 0.5));
console.log(Math.pow(9, 1/3));
console.log(Math.pow(25, 1.5));
2.23606797749979
2.080083823051904
125
Negative Base value with positive fractional exponent
In the example below, we use the pow() function with a negative base value and a positive fractional exponent.
console.log(Math.pow(-5, 1/2));
console.log(Math.pow(-10, 0.5));
console.log(Math.pow(-10, 0.75));
NaN
NaN
NaN
Negative Base value and a Positive integer exponent
In the following example, we use the JavaScript pow() function with a negative base value and a positive integer as the exponent value.
- If the exponent value is an even number, the pow() function returns a positive output.
- If the exponent value is an odd number, the method returns a negative output.
The code below shows the pow() method result for even and odd exponent values.
console.log(Math.pow(-5, 3));
console.log(Math.pow(-5, 4));
-125
625
Positive/Negative Base value with Negative Exponent
The Math.pow() function works if we use a negative exponent value in combination with a positive or negative base value.
console.log(Math.pow(25, -3));
console.log(Math.pow(-100, -2));
console.log(Math.pow(100, -0.5));
0.000064
0.0001
0.1
JavaScript Math.pow() with zero arguments
As we all know, any number raised to the power of 0 (40) becomes 1. Similarly, 0 raised to any number becomes (04) 1.
console.log(Math.pow(0, 0));
console.log(Math.pow(0, 3));
console.log(Math.pow(3, 0));
console.log(Math.pow(0, 0.5));
console.log(Math.pow(-0, 1));
console.log(Math.pow(-0, 1.5));
console.log(Math.pow(-0, -1));
console.log(Math.pow(-0, -0.5));
1
0
1
0
-0
0
-Infinity
Infinity
What is the difference between Math.pow() and the ** operator in JavaScript?
Both the built-in Math.pow() function and the exponent (**) operator are useful to find the number raised to a power and, in most cases, return the same result.
We can use Math.pow() or the ** operator on a positive base with a negative exponent or a fractional exponent.
console.log(Math.pow(4, 3));
console.log(4 ** 3);
console.log(Math.pow(4, 0.5));
console.log(4 ** 0.5);
console.log(Math.pow(4, -3));
console.log(4 ** -3);
64
64
2
2
0.015625
0.015625
Using a negative base value
While using the negative base value, we must be careful with the negative sign in the exponent operator.
console.log(Math.pow(-2, 3));
console.log((-2) ** 3);
console.log(-(2 ** 3));
-8
-8
-8
Related Math Posts