Table of Contents
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
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
4^3 might look like this.
let exp = 4 * 4 * 4; console.log(exp);
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 -
** - 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)
** 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);
** operator works
let exp = BigInt(345) ** BigInt(3); console.log(exp);
Also, it works with floating-point numbers
let exp = 4 ** 3.3; console.log(exp);
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);
Use pow method for exponentiation
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));
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
** 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.