In JavaScript, to get the value of a number and its exponent, we can use the **Math.pow()** method.

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

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

Some other examples of Math.pow() method are below:

```
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);
```

Which would result in the following:

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

Note that we can also use the **exponentiation operator (**)** to get the same results. Here is an example:

`var num = 2**4;`

The above code will give the same result as our first example of Math.pow(2,4) = 16.

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

## JavaScript Exponents in Action

Below we will provide code to let the user input two numbers, and then use the **Math.pow()** method 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="text" value1="">
<input id="userVal2" type="text" 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;
var num = Math.pow(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="text" value1="">
<input id="userVal2" type="text" 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;
var num = Math.pow(Number(num1), Number(num2));
document.getElementById("results").textContent = num;
}
</script>
```

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

## Leave a Reply