DEV Community

Cover image for Exponentiation in JavaScript: A Beginner’s Guide
Mohamed Mayallo
Mohamed Mayallo

Posted on • Updated on • Originally published at mayallo.com

Exponentiation in JavaScript: A Beginner’s Guide

Introduction

Exponentiation refers to a mathematical process of multiplying a number by itself, raised to the power of another number.

If, for instance, we raise 2 to the power of 3, we calculate it as 2 * 2 * 2, which gives us the result of 8.

In JavaScript, you can use either the ** operator introduced in ES6 or the method Math.pow() when evaluating exponents.

Before starting, please keep in mind that this operator ^ is the Bitwise XOR Operator. Don't use it for exponentiation, as it is a common confusion.

Using The ** Operator

The ** operator is used to perform exponentiation in JavaScript. It takes two operands: the base and the exponent.

The base (the left-hand side) is the number that is being raised to power, and the exponent (the right-hand side) is the power itself.

Have a look at the following example:

let result = 2 ** 3 // 8; 
Enter fullscreen mode Exit fullscreen mode

In this example, 2 is the base and 3 is the exponent. The ** operator raises 2 to the power of 3, which is 8.

The Precedence Of The ** Operator

Keep in mind, the ** operator has higher precedence than the multiplication and division operators.

This means that if you have an expression that includes both multiplication and exponentiation, the exponentiation will be evaluated first.

Here’s an example:

let result1 = 2 * 3 ** 2, // 18
    result2 = (2 * 3) ** 2; // 36
Enter fullscreen mode Exit fullscreen mode

In this example, regarding result1, 3 is raised to the power of 2 first, resulting in 9. Then, the multiplication is performed, resulting in a final value of 18.

But if you want to precede the multiplication operator in the case of result2, you have to enclose the multiplication operation between ().

Another example, if you want to find the nth roots:

let result1 = 8 ** 1 / 3, // 2.6666666666666665
    result2 = 8 ** (1 / 3); // 2
Enter fullscreen mode Exit fullscreen mode

Using Math.Pow() Method

In addition to the ** operator, JavaScript also provides the Math.pow() method for performing exponentiation.

Like the ** operator, this method takes two arguments: the base and the exponent.

Here’s an example of how to use Math.pow():

let result = Math.pow(2, 3); // 8
Enter fullscreen mode Exit fullscreen mode

In this example, 2 is the base and 3 is the exponent. The Math.pow() method raises 2 to the power of 3, which is 8.

Math.pow() Method vs ** Operator

Actually, there is a slight difference between them regarding the BigInt type.

The Math.pow() method doesn't support the BigInt type, however, the ** operator supports it. Take a look at the following example:

let result1 = 2n ** 4n, // 16n
  result2 = Math.pow(2n, 4n); // Uncaught TypeError: Cannot convert a BigInt value to a number
Enter fullscreen mode Exit fullscreen mode

In JavaScript, the BigInt is created like 2n or BigInt(2)

Which One Should You Use?

As you saw, there are no big differences between Math.pow() and the ** operator.

However, if you work with BigInt values, go ahead with ** operator, rather than such a case use whatever you want.

Keep in mind, if you chose the ** operator, just take care of the precedence.

Conclusion

Exponentiation is a fundamental mathematical operation. And, in JavaScript, exponentiation can be performed using the ** operator or the Math.pow() method.

In this article, we knew how to use both, the ** operator and the Math.pow() method.

Then, we knew that there are no big differences between them, so it is up to you to use either of them.

Before you leave

If you found this article useful, check out these articles as well:

Thanks a lot for staying with me up till this point. I hope you enjoy reading this article.

Top comments (11)

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli • Edited

There is one big difference between them. Math.pow only works for regular numbers. ** also works for BigInts.

To quote the MDN page on **:

The ** operator is overloaded for two types of operands: number and BigInt. It first coerces both operands to numeric values and tests the types of them. It performs BigInt exponentiation if both operands becomes BigInts; otherwise, it performs number exponentiation. A TypeError is thrown if one operand becomes a BigInt but the other becomes a number.

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

Actually, there are no big differences between Math.pow() and the ** operator.

You're wrong, there is one big difference Math.pow() works only on number but ** operator works also for BigInt.

Image description

Collapse
 
mayallo profile image
Mohamed Mayallo

Yes you are right, I meant a big difference for the daily basis work.
Anyway, I am going to add this tip.

Collapse
 
not-ethan profile image
Ethan

Never knew they added a Math function for exponents. IMO using ** is better since its like that in other langues like Python, Ruby and probably others. Though Math.pow() is also used in Java and they dont have **

Collapse
 
mayallo profile image
Mohamed Mayallo

Agree with you, ** is better and easier.

Collapse
 
pchol22 profile image
Pierre Chollet

Nice!

Also good to know, contrary to what some people may think, the ^ operator isn't exponentiation at all (it is sometimes used as such, for example in the google calculator), its a XOR operator and will yield totally different results!

Collapse
 
mayallo profile image
Mohamed Mayallo

Thanks, good tip!

Collapse
 
ryencode profile image
Ryan Brown

Also of note would be that performance wise these two methods seem to be equal (within a small margin)

Math.pow vs ** vs *

Collapse
 
ant_f_dev profile image
Anthony Fung

Thanks for sharing - I knew about Math.pow(), but not about **.

Collapse
 
mayallo profile image
Mohamed Mayallo

Glad to know it helps!

Collapse
 
philipjohnbasile profile image
Philip John Basile

Neat!