The result obtained from elevating the first input value to the power of the second input value is produced by the exponentiation (**) operator. This operator functions similarly to Math.pow, but it additionally allows BigInts as operands.
The exponentiation operator in JavaScript serves the purpose of computing the exponential value based on the specified power data. It acts as an alternative to the Math.pow function, offering an improved method for performing calculations. This operator allows the use of positive and negative numbers, as well as various numerical inputs for computations.
Syntax
The syntax provided represents the symbol required for utilizing the exponentiation operator.
First_value ** second_value
- The "First_value" is the input value of the operation.
- The "second_value" is the power of the given input value.
- The first and second input values accept as number data per requirement.
- The math.pow operator does not allow bigint data for the calculation. It accepts a value and converts it into an integer value.
- The exponentiation (**) operator is used given value as a number type. Other data neglect or does not use for the calculation.
- The math.pow operator does not use the bigint value. The exponentiation operator accepts the bigint value.
- Let's see the example of the Math.pow and exponentiation (**) operator. The input values are different for both operators.
Drawback of the Math.pow Operator
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"></p>
<script>
let result = 2n ** 3n;
console.log(result);
let result2 = Math.pow(2,3);
document.getElementById("demovalue").innerHTML =
"The '**' operator: "+ result+"<br> The 'Math.pow()' operator:"+result2;
</script>
</body>
</html>
Output
The image below illustrates the various input values alongside their corresponding output data.
Examples
The subsequent examples illustrate the exponentiation operator along with its operational mechanism.
Example 1 :
The fundamental operator for exponentiation and its operational mechanism are illustrated below.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"> </p>
<p id = "demovalue1"> </p>
<p id = "demovalue2"> </p>
<p id = "demovalue3"> </p>
<script>
let result = 4 ** 4;
let result2 = 4 ** 5;
let result3 = 4 ** 15;
let result4 = 14 ** 5;
document.getElementById("demovalue").innerHTML =
"The '4**4' operator: "+ result;
document.getElementById("demovalue1").innerHTML =
"The '4**5' operator: "+ result2;
document.getElementById("demovalue2").innerHTML =
"The '4**15' operator: "+ result3;
document.getElementById("demovalue3").innerHTML =
"The '14**5' operator: "+ result4;
</script>
</body>
</html>
Output
The fundamental value of the operator is illustrated in the accompanying output image.
Example 2 :
The fundamental exponentiation operator utilizing a negative input value is demonstrated below.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"> </p>
<p id = "demovalue1"> </p>
<p id = "demovalue2"> </p>
<p id = "demovalue3"> </p>
<p id = "demovalue4"> </p>
<p id = "demovalue5"> </p>
<script>
let result = (-4) ** 4;
let result2 = (-4) ** 5;
let result3 = 4 ** (-5);
let result4 = 4 ** (-4);
let result5 = (-4) ** (-4);
let result6 = (-4) ** (-5);
document.getElementById("demovalue").innerHTML =
"The '(-4) ** 4' operator: "+ result;
document.getElementById("demovalue1").innerHTML =
"The '(-4) ** 5' operator: "+ result2;
document.getElementById("demovalue2").innerHTML =
"The '4 ** (-5)' operator: "+ result3;
document.getElementById("demovalue3").innerHTML =
"The '4 ** (-4)' operator: "+ result4;
document.getElementById("demovalue4").innerHTML =
"The '(-4) ** (-4)' operator: "+ result5;
document.getElementById("demovalue5").innerHTML =
"The '(-4) ** (-5)' operator: "+ result6;
</script>
</body>
</html>
Output
The fundamental values associated with the operator can be observed in the output image provided.
Example 3: The fundamental exponentiation operator with significantly large values is demonstrated in the following example.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"> </p>
<p id = "demovalue1"> </p>
<p id = "demovalue2"> </p>
<p id = "demovalue3"> </p>
<script>
let result = (-411111) ** (2);
let result2 = 2 ** 1225;
let result3 = 2 ** 225;
let result4 = 54115311 ** (-4);
document.getElementById("demovalue").innerHTML =
"The '(-411111) ** (2)' operator: "+ result;
document.getElementById("demovalue1").innerHTML =
"The '2 ** 1225' operator: "+ result2;
document.getElementById("demovalue2").innerHTML =
"The '2 ** 225' operator: "+ result3;
document.getElementById("demovalue3").innerHTML =
"The '54115311 ** (-4)' operator: "+ result4;
</script>
</body>
</html>
Output
The fundamental value of the operator is displayed in the output image.
Example 4 :
Below is an illustration of the multiple exponentiation operator accompanied by input data.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"> </p>
<p id = "demovalue1"> </p>
<p id = "demovalue2"> </p>
<p id = "demovalue3"> </p>
<p id = "demovalue4"> </p>
<script>
let result = 2 ** 2 ** 2;
let result2 = 4 ** 5 ** 2;
let result3 = 2 ** 2 ** 3 ** 1;
let result4 = 2 ** 2 ** (-3) ** 1;
let result5 = 2 ** (-3) ** (-3) ** 1;
document.getElementById("demovalue").innerHTML =
"The '2 ** 2 ** 2' operator: "+ result;
document.getElementById("demovalue1").innerHTML =
"The '4 ** 5 ** 2' operator: "+ result2;
document.getElementById("demovalue2").innerHTML =
"The '2 ** 2 ** 3 ** 1' operator: "+ result3;
document.getElementById("demovalue3").innerHTML =
"The '2 ** 2 ** (-3) ** 1' operator: "+ result4;
document.getElementById("demovalue4").innerHTML =
"The '2 ** (-3) ** (-3) ** 1' operator: "+ result5;
</script>
</body>
</html>
Output
The fundamental value of the operator is depicted in the resulting image.
Example 5 :
The fundamental exponentiation operator utilizing bigint data is demonstrated in the example provided below.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"> </p>
<p id = "demovalue1"> </p>
<p id = "demovalue2"> </p>
<p id = "demovalue3"> </p>
<script>
let result = 3n ** 3n;
let result2 = 4n ** 5n;
let result3 = 3n ** 3n ** 3n;
let result4 = 4n ** 5n ** 2n;
document.getElementById("demovalue").innerHTML =
"The '3n ** 3n' operator: "+ result;
document.getElementById("demovalue1").innerHTML =
"The '4n ** 5n' operator: "+ result2;
document.getElementById("demovalue2").innerHTML =
"The '3n ** 3n ** 3n' operator: "+ result3;
document.getElementById("demovalue3").innerHTML =
"The '4 ** (-4)' operator: "+ result4;
document.getElementById("demovalue4").innerHTML =
"The '(-4) ** (-4)' operator: "+ result5;
document.getElementById("demovalue5").innerHTML =
"The '4n ** 5n ** 2n' operator: "+ result6;
</script>
</body>
</html>
Output
The fundamental value of the operator is displayed in the output image.
Example 6 :
The fundamental exponentiation operator in conjunction with other operators is illustrated below.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"> </p>
<p id = "demovalue1"> </p>
<p id = "demovalue2"> </p>
<p id = "demovalue3"> </p>
<p id = "demovalue4"> </p>
<script>
let result = 3;
result **= 2;
let result2 = (-3);
result2 **= 3;
let result3 = (-3);
result3 **= 4;
let result4 = 5;
result4 **= result4;
let result5 = 3+6;
result5 **= 3;
document.getElementById("demovalue").innerHTML =
"The exponentiation operator: "+ result;
document.getElementById("demovalue1").innerHTML =
"The exponentiation operator: "+ result2;
document.getElementById("demovalue2").innerHTML =
"The exponentiation operator: "+ result3;
document.getElementById("demovalue3").innerHTML =
"The exponentiation operator: "+ result4;
document.getElementById("demovalue4").innerHTML =
"The exponentiation operator: "+ result5;
</script>
</body>
</html>
Output
The fundamental value of the operator is illustrated in the output image.
Syntax Error for the Exponentiation Operator
Negative values must be enclosed within parentheses. If we attempt to use them in the same manner as positive input data, JavaScript will display a syntax error in the console.
Let us examine an instance of a syntax error involving the exponentiation operator.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript exponentiation operator</h2>
<p id = "demovalue"> </p>
<p id = "demovalue1"> </p>
<script>
let result = -3 ** 2;
document.getElementById("demovalue").innerHTML =
"The exponentiation operator: "+ result;
</script>
</body>
</html>
Output
The output image displays the syntax error associated with the operator.
Conclusion
The exponentiation operator represents the most advanced and recent method for computing exponential values. This operator is designed to be straightforward, uncomplicated, and accessible for both users and developers in JavaScript.