In JavaScript, the Power Operator, or exponentiation operator, ******, is used to raise a number to the power of an exponent.

`var num = 2**4;`

The output of the code above would be the number 16.

Some other examples of the power operator are below:

```
var num1 = 6**1;
var num2 = 2**2;
var num3 = 3**25;
var num4 = 0**3;
var num5 = (-2)**2;
var num6 = (-2)**3;
var num7 = (-2)**-2;
var num8 = (-2)**-3;
var num9 = 3**0;
var num10 = .3**4;
```

This would result in the following:

```
6
4
847288609443
0
4
-8
0.25
-0.125
1
0.0081
```

One main thing to be aware of is that any **negative base number** will need to be in parentheses. So **(-2)**2** and not -2**2.

Note that we can also use the Math.pow() method to get the same result.

`var num = Math.pow(2,4);`

The output of the code above would again be the number 16.

Let’s see the examples above using the Math.pow() method:

```
var num1 = Math.pow(6,1);
var num2 = Math.pow(2,2);
var num3 = Math.pow(3,25);
var num4 = Math.pow(0,3);
var num5 = Math.pow(-2,2);
var num6 = Math.pow(-2,3);
var num7 = Math.pow(-2,-2);
var num8 = Math.pow(-2,-3);
var num9 = Math.pow(3,0);
var num10 = Math.pow(.3,4);
```

They result in the same output as we had using the power operator above:

```
6
4
847288609443
0
4
-8
0.25
-0.125
1
0.0081
```

## JavaScript Power Operator in Action

Below we will provide code to let the user input two numbers, and then use the **power operator** to find the first number(base) to the power of the second number(exponent). Here is our simple HTML setup:

```
<p>Enter two numbers below. We will provide the result of the first number to the power of the second number.</p>
<input id="userVal1" type="number" value1="">
<input id="userVal2" type="number" value2="">
<button onclick="runFunction()">Submit</button>
<div id="results"></div>
```

Below we will write our function. We will get both numbers that the user has entered using the value property. We will then display the results using the textContent property.

```
function runFunction() {
var num1 = document.getElementById("userVal1").value;
var num2 = document.getElementById("userVal2").value;
if( num1 < 0 ){
var num = (Number(num1))**Number(num2);
} else {
var num = Number(num1)**Number(num2);
}
document.getElementById("results").textContent = num;
}
```

The final code and output for this example is below:

Code Output:

Enter two numbers below. We will provide the result of the first number to the power of the second number.

Full Code:

```
<p>Enter two numbers below. We will provide the result of the first number to the power of the second number.</p>
<input id="userVal1" type="number" value1="">
<input id="userVal2" type="number" value2="">
<button onclick="runFunction()">Submit</button>
<div id="results"></div>
<script>
function runFunction() {
var num1 = document.getElementById("userVal1").value;
var num2 = document.getElementById("userVal2").value;
if( num1 < 0 ){
var num = (Number(num1))**Number(num2);
} else {
var num = Number(num1)**Number(num2);
}
document.getElementById("results").textContent = num;
}
</script>
```

Hopefully this article has been useful in helping you learn about JavaScript Power Operator.

## Leave a Reply