## Introduction

Mathematics is everywhere within programming and languages like JavaScript, and its ability to carry out complex mathematical computations is one of the many reasons why we use them. Typical mathematical operations such as addition, subtraction, multiplication, and division are possible, but also, you can carry out other advanced mathematical operations in JavaScript such as exponentiation.

In this article, we will show how to execute exponentiation operations within JavaScript

## What is Exponentiation in Mathematics?

Exponentiation is the process of raising a numerical quantity to the power of another where the numerical quantity is considered the `base`

, and the other number is called the `exponent`

(or `power`

).

If the exponent is a positive integer, the exponentiation formula is defined as below

```
b^n = b * b *...* b * b (where the numbers of b is equal to n)
b^3 = b * b * b
b^5 = b * b * b * b * b
```

So, if you donโt know the exponentiation operator, your JavaScript code for the `4^3`

might look like this.

```
let exp = 4 * 4 * 4;
console.log(exp);
```

Output

`64`

However, if we need to raise 4 to the power of 20, it will not make sense to type twenty `4`

. Thatโs where the exponentiation operator comes in. So to summarize, Exponentiation means raising a certain base number to the power of the exponent, for example, `x^y`

. This can be read as `x`

to the power of `y`

. It means that we will multiply `x`

by itself `y`

number of times.

## The Exponentiation operator (**) in JavaScript

The exponentiation operator - `**`

- allows us to raise the first operand to the power of the second operand and it supports `BigInt`

type. In addition, the exponentiation operator is right-associative and so if there are more than one exponentiation, it executes the exponentiation operation from the right.

```
a * b * c = a * (b * c)
```

The `**`

operator holds a higher precedence over `+`

, `_`

, `/`

, `*`

. and therefore will become their operands.

Now, letโs see the `**`

operator in action

```
let exp = 4 ** 3;
console.log(exp);
```

Output

`64`

Also, the `**`

operator works `BigInt`

values.

```
let exp = BigInt(345) ** BigInt(3);
console.log(exp);
```

Output

`41063625n`

Also, it works with floating-point numbers

```
let exp = 4 ** 3.3;
console.log(exp);
```

Output

`97.00586025666546`

However, you need to understand that especially with floating-point numbers, there is approximation going on and therefore will not be fully accurate.

If you want to make use of a negative number as your base, you will have to make use of a bracket to force the expression as it would throw an error - **Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence.**

```
console.log((-2) ** 5);
```

Output

`-32`

## Use pow method for exponentiation

Within the `Math`

object, you can make use of the `pow`

method to achieve exponentiation where it takes two arguments, the base number and the exponent.

Letโs take a look at some examples to see the `pow`

method in action

```
console.log(Math.pow(4, 5));
console.log(Math.pow(6, -2));
console.log(Math.pow(-2, 5));
```

Output

```
1024
0.027777777777777776
-32
```

Unlike with the exponentiation operator, it doesnโt require you to force negative numbers with brackets. Itโs similar to the `**`

operator, however, it only works with numbers and not `BigInt`

types.

## Summary

There are two ways to achieve exponentiation in JavaScript, the `**`

operator and the `pow`

method. With the `**`

operator, you can work with both Numbers and BigInt values, however, with the `pow`

method, you can only work with Numbers.

## References

Exponentiation (**) - JavaScript | MDN (mozilla.org)

Exponentiation - Wikipedia

Operator precedence - JavaScript | MDN (mozilla.org)

Exponentiation (**) - JavaScript | MDN (mozilla.org)