x ** y
** 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.
For both numbers and BigInts,
0 raised to a positive power returns
0 raised to a power of
1. For numbers,
0 raised to a negative number returns
-0 raised to a negative number returns
NaN ** 0 (and the equivalent
Math.pow(NaN, 0)) is the only case where
NaN doesn't propagate through mathematical operations — it returns
1 despite the operand being
NaN. In addition, the behavior where
base is 1 and
exponent is non-finite (±Infinity or
NaN to preserve backward compatibility with its original behavior.
For BigInt exponentiation, a
RangeError is thrown if the exponent
y is negative. This is because any negative exponent would likely result in a value between 0 and 1 (unless the base is
0), which is rounded to zero, and is likely a developer mistake.
The exponentiation operator is right-associative:
a ** b ** c is equal to
a ** (b ** c).
In most languages, such as PHP, Python, and others that have an exponentiation operator (
**), the exponentiation operator is defined to have a higher precedence than unary operators, such as unary
+ and unary
-, but there are a few exceptions. For example, in Bash, the
** operator is defined to have a lower precedence than unary operators.
await) immediately before the base number; doing so will cause a SyntaxError.
-(2 ** 2) — to make the intention unambiguous.
Note that some programming languages use the caret symbol
2 ** 3; // 8 3 ** 2; // 9 3 ** 2.5; // 15.588457268119896 10 ** -1; // 0.1 2 ** 1024; // Infinity NaN ** 2; // NaN NaN ** 0; // 1 1 ** Infinity; // NaN 2n ** 3n; // 8n 2n ** 1024n; // A very large number, but not Infinity 2n ** 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions // To do exponentiation with a BigInt and a non-BigInt, convert either operand 2n ** BigInt(2); // 4n Number(2n) ** 2; // 4
2 ** 3 ** 2; // 512 2 ** (3 ** 2); // 512 (2 ** 3) ** 2; // 64
To invert the sign of the result of an exponentiation expression:
-(2 ** 2); // -4
To force the base of an exponentiation expression to be a negative number:
(-2) ** 2; // 4
|ECMAScript Language Specification |
BCD tables only load in the browser