

Exponentiation in JavaScript: A Beginner’s Guide
source link: https://mayallo.com/exponentiation-in-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Exponentiation in JavaScript: A Beginner’s Guide
Exponentiation refers to a mathematical process of multiplying a number by itself. In JavaScript, you can perform it using the ** operator or Math.pow() method.
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.
Using the ** Operator
The **
operator is used to performing 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
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
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
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
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
.
Which One Should You Use?
Actually, there are no big differences between Math.pow()
and the **
operator.
Simply, go with any of them but if you choose 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.
Think about it
If you liked this article please rate and share it to spread the word, really, that encourages me a lot to create more content like this.
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.
Recommend
-
60
Go programming language invalid modular exponentiation result (Exp() in math/big pkg) Nmap Announce
-
43
PrefaceHi! My name is Alexander Borzunov . This article is the translation of my original publication . Sharing my work with Englis...
-
78
What is Closure? A closure is a function that has access to its outer function scope even after the outer function has returned. This means a closure can remember and access variables and arguments of its outer...
-
18
寫程式應該很常會用到指數運算,過去我們會用 Math.pow(),但在 ES2016 (ES7) 提供了 exponentiation operator (指數運算子) 的讓寫法更...
-
6
POJ 1001 - Exponentiation | 眈眈探求 POJ 1001 - Exponentiation Time: 1000MS Memory: 10000K 难度:...
-
6
About exponentiation-distance on DAGs About exponentiation-distance on DAGs
-
5
Fast Exponentiation Using Bit Masking using C++ Filed Under: C++In this article, we will learn fast exp...
-
8
[Golang] Integer Exponentiation May 20, 2017 Integer exponentiation in Go programming language.
-
7
Guess.Who's blog Looking For Dp +...
-
3
Can I use Binary Exponentiation with this problem? (2^2^n + 1) mod k Can I use Binary Exponentiation with this problem? (2^2^n + 1)...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK