power operator in javascript

Power Operator in JavaScript and The Best 5 Examples

The exponentiation operator in JavaScript(Power Operator in JavaScript), introduced in ECMAScript 2016 (ES7), is a concise way to exponentiate numbers in the JavaScript programming language. This operator is a suitable replacement for the Math.pow javascript ” Math.pow ()‎” For the purpose of power calculations, it is considered.

The ” Syntax ” of this ” Operator “ is very simple and increases the readability of the code, and on the other hand, makes mathematical operations more efficient in JavaScript programming. In this educational article from “Thinkwriteway Technology Blog“, this time, we intend to provide good explanations about the power operator in JavaScript programming language and examine several practical examples of it in a simple and fluent language.

What is the exponentiation operator in JavaScript?

JavaScript provides a simple approach to performing calculations on the exponentiation of numbers by using the exponentiation operator, represented by two asterisks “**”. This operator provides the process of exponentiation of numbers and requires a static Math.pow javascript method “Math.pow ()” Eliminates more complex syntax.

Syntax of the Math.pow javascript method “Math.pow ()” To power a specific number in a specific base is as follows:

Math.pow(base, exponent)

This method can be understood by looking at the example below:

let result = Math.pow(2,2);
console.log(result); // 4

result = Math.pow(2,3);
console.log(result); // 8

In ECMAScript 2016, a new way to exponentiate numbers was introduced. This method introduces the power operator in javascript (**) came about, where a completely new syntax is used to do this. This operator is an alternative to the conventional approach of using a function Math.pow javascript “Math.pow ()” Its syntax is as follows:

base ** exponent

In the above syntax, base It means base number and exponent It is also its view. For example, if base equal to 3 and exponent is equal to 4, as a result, the above command increases the number 3 to the power of 4, which will be the result of calculating the number 27. Now using the Math.pow javascript method “Math.pow ()” It is not common for such calculations and the exponentiation operator is used in JavaScript instead. The exponentiation operator in Javascript increases the clarity and efficiency of the code. This operator is demonstrated in the example below:

let result = 2 ** 2;
console.log(result); // 4

result = 2 ** 3;
console.log(result); // 8

The power operator not only aligns with the evolving syntax of JavaScript, but also contributes to a more understandable and easier coding experience.

power operator in javascript

Note: While power is a common mathematical operation, some programming languages ​​use the symbol ^ They use it for this purpose. However, in the case of JavaScript, this symbol plays a different role, as the symbol for the “XOR” operator, as one of the bitwise operators. A noteworthy point regarding the decision of JavaScript developers to use the signed exponentiation operator ** Instead of using the icon ^ It shows the language’s commitment to providing consistent and distinct syntax for different operators, which ultimately increases code clarity.

Example of exponentiation operator in JavaScript

The following example is given to better understand the exponentiation operator in JavaScript:

let result = 2 ** 2;
console.log(result); // 4

result = 2 ** 3;
console.log(result); // 8

Contrary to the method “Math.pow ()” which requires value conversion for calculation, exponentiation operator is designed to work with numerical values. Also, this operator is compatible with “Bigint” data type, which can be seen in the following scenario.

let result = 2n ** 3n;
console.log(result); // 8n

Note: BigInt numbers are used in JavaScript to represent integers of arbitrary length. These numbers are another type of Number data type in JavaScript and are sometimes needed because ordinary numbers cannot be easily converted from 253 be more or less than -253 to be

let result = 2n ** 3n;
console.log(result); // 8n

It is worth noting that the power operator in JavaScript is not limited to independent expressions and this symbol operator infix It also has usability and integration, which the following example expresses this concept:

let x = 2;
x **= 4;
console.log(x); // 16

Basically, the exponentiation operator in JavaScript, in relation to its counterpart i.e., Math.pow javascript “Math.pow ()”, provides a stronger basis for calculating exponential values. This operator, whether in standard or BigInt form, will help the versatility of JavaScript in managing various numerical calculations.

Common errors in using power operator in JavaScript

Regarding the exponentiation operator in JavaScript, it is important to note that it is forbidden to place some kind of “unary” operator directly before the base number. Any attempt to do so will result in a syntax error. The following example illustrates this concept:

let result = -2**3; // Causes SyntaxError

The above code produces the following error.

Uncaught SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence

To correct this situation, parentheses should be used to prioritize calculations. The parentheses used in this approach effectively resolve the ambiguity of operator precedence, as the following example illustrates this concept:

let result = (-2)**3;
console.log(result); // -8

Using parentheses to do this is a caution for using JavaScript syntax rules and emphasizes the importance of properly prioritizing parenthesized expressions to ensure smooth, error-free execution.

power operator in javascript

The explanation and example given above means that when a user wants to use a Unary operator immediately before a number (such as a minus sign (-)), JavaScript syntax rules do not allow it. This is because JavaScript interprets the expression in a way that may lead to ambiguity or unexpected behavior. For example, the phrase -2**3 consider These calculations can be interpreted in two different ways: as – (2 ** 3) (which means the negative result of expression 2 to the power of 3) or as (2-) ** 3 (which means raising a negative number to the power of 2 to the power of 3). To avoid this ambiguity, JavaScript requires the user to use parentheses to explicitly specify the order of operations.

An example for prioritizing operators in exponentiation

When an expression in the form a ** b ** c is present, the operations are evaluated from right to left, meaning that in the preceding expression, the result of the expression a ** (b ** c) it will be counted. For example, variables a = 5 و “b = 2” ، c = 2 are assumed and the following code fragment is intended for them:

a ** b ** c; // Evaluates to 625
// Execution order of a ** b ** c;
// (5 ** (2 ** 2) )
// (5 ** 4)
// 625

As mentioned, JavaScript enforces a rule in which the user cannot use certain unary operators (+/-/~/! /delete/void/type of) immediately before the base number in such expressions. Trying to do that will cause a syntax mistake. For example, the following operations are invalid for the exponentiation operator in JavaScript and will generate an error:

+a ** b; // Invalid
-a ** b; // Invalid
~a ** b; // Invalid
!a ** b; // Invalid
delete a ** b; // Invalid
void a ** b; // Invalid
typeof a ** b; // Invalid

Each of these operations leads to some kind Error Syntax Uncaught because in them the unary operator is used immediately before the exponentiation operator declaration. In this situation, as stated, parentheses should be used to prioritize operator

Dealing with power operator in JavaScript with negative values

Dealing with negative numbers in the context of using the exponentiation operator in Javascript requires careful consideration. The following example is very important to understand this concept.

-2 ** 2; //invalid
// The above expression can be converted into 
(-2) ** 2; // 4
(-2) ** 3; //-8 

In the example above, use the expression -2 ** 2 It is directly invalid due to possible ambiguity. To clarify the purpose, it can be defined as (-2) ** 2 Rewrote that to the number 4 will be evaluated. Similarly, when working with a negative base in a power, e.g. (-2) ** 3, the result is equal to -8 will be. By using the parentheses capabilities, explicit interpretation of exponentiation operations is possible in JavaScript.

Dealing with exponentiation operator in Javascript with NAN values

Performing exponentiation operations in JavaScript including “NaN (Not-a-Number)” results in a value being retrieved NaN It is possible that the following example is given to express this concept.

NaN ** 1; //NaN
NaN ** NaN;  // NaN

In both cases, any arithmetic operation with NaN be done as a type of operand, NaN gives as output to the user. This behavior reflects the inherent nature NaN which represents undefined values in numerical calculations.

power operator in javascript

Dealing with exponentiation operator in JavaScript with Undefined values

When using the “undefined” value (Undefined) in power operations, such as 1 ** undefined, Result NaN The following example is given to express this concept:

1 ** undefined; // NaN

This behavior originates from the fact that when in numerical calculations of Undefined is used, this value Undefined implicitly to the value NaN will be converted. As a result, the expression to 1 ** NaN Turned that leads to the result NaN will be

Exponentiation of null values

When applying the exponentiation operator in JavaScript to a “Null” value, the behavior is similar to how this operator interacts with the value Undefined will be. Here is a sample to show this idea:

10 ** null; // 1
// because
10 ** Number(null); // Number(null) --> 0
10 ** 0; // 1

In the example above Number(null) To 0 is evaluated, resulting in any number to the power of 0 reach leads to recovery 1 will be. Therefore, the expression 10 ** null To 10 ** 0 Converted and amount 1 is restored. This phenomenon emphasizes the mathematical law that every basic number to the power 0 reach the output result of the number 1 will be.

Frequently asked questions about the exponentiation operator in JavaScript

In this section, some of the common errors related to the power operator in JavaScript will be presented. Paying attention to these questions will greatly help users to understand the power operator.

How to exponentiate numbers in JavaScript?

There are two ways to exponentiate numbers in JavaScript. The first method is to use the Math.pow method, which accepts two operands, the first operand is the base number and the second operand is the power operator. It is also possible to use the exponentiation operator, which is indicated by an asterisk, between the base number and the exponent.

Math.pow or exponent operator is better for performing power operations?

For performing exponentiation operations in JavaScript, the exponentiation operator is often preferred over the Math.pow method due to its concise syntax and readability. The exponentiation operator in JavaScript directly performs operations on exponentiation of numbers, thereby helping code clarity. This operator essentially eliminates the need for a separate function call and improves code efficiency.


The exponentiation operator is used as one of the javascript operators to exponentiate the base number to a specific exponent and is a very suitable replacement for the Math.pow () Is. This operator is versatile and can handle numeric values and even Bigint values. In the above article from Thinkwriteway Technology Blog, relatively favorable information about the power operator in Javascript was expressed and this operator was used in various scenarios, including being used in numerical calculations and even Bigint calculations.

Also, in the above article from Thinkwriteway Technology Blog, this operator is used in different situations such as using with negative values, using with undefined values, using with null values and using with values NaN was investigated. Compatibility of power operator in JavaScript with different types of values shows its importance in different programming fields. I hope that the above content has been satisfied by dear users.

If you seek further information about Power Operator in JavaScript and The Best 5 Examples  or have any inquiries regarding our blog, please don’t hesitate to contact us via email at info@thinkwriteway.com

Share This Article :

Leave a Comment

What to read next