JavaScript exponentiation operator (**) will return the first operand’s power to the second operand. ECMAScript 2016 provided an alternative way to get a base to the exponent power by using the exponentiation operator ( **
) with the following syntax:
x**n
The operator **
raises the x
to the power of an exponent n
.
Note: a ** b is equivalent to aba^{b}ab, which is equivalent to Math.pow(a, b)
JavaScript Exponentiation operator
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
let res1 = 2 ** 20;
console.log(res1);
var res2 = 2 ** 3;
console.log(res2);
</script>
</body>
</html>
Output:
This operator **
also accepts the numbers of the bigint
let result = 2n ** 3n;
console.log(result); // 8n
More Examples
console.log(3 ** 4); // 81
console.log(10 ** -2); // 0.01
console.log(2 ** 3 ** 2); // 512
console.log((2 ** 3) ** 2); // 64
Invalid Operations
You cannot put a unary operator (+/-/~/!/delete/void/typeof
) immediately before the base number.
+a ** b; -a ** b; ~a ** b; !a ** b; delete a ** b; void a ** b; typeof a ** b;
All the above operations are invalid and result in
Uncaught SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence
Do comment if you have any doubts or suggestions on this Js operator topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version