Powers
Compute powers directly in numeric expressions.
JS Puzzles · Const
The exponentiation operator ** raises a base to an exponent, including fractional exponents. It is right-associative, so chained expressions group from the right.
The ** operator raises a base value to an exponent. JavaScript evaluates chained exponentiation from right to left, so a ** b ** c means a ** (b ** c); fractional exponents can express roots.
a ** b| Name | Type | Description |
|---|---|---|
a | number or BigInt | The base value to raise to a power; it is coerced to a numeric value. |
b | number or BigInt | The exponent applied to the base; it is coerced to a numeric value. |
The result of raising the first operand to the power of the second operand.
2 ** 3 ** 2 is 2 ** (3 ** 2), producing 512.const squareRoot = 4 ** (1 / 2);
const nested = 2 ** 3 ** 2;
console.log(squareRoot); // 2
console.log(nested); // 512The operator raises its left operand to the power of its right operand; chaining groups from right to left.
Compute powers directly in numeric expressions.
Express roots using fractional exponents, such as value ** (1 / 2).
Use right-associative chaining when one exponent depends on another.
** raises the first operand to the power of the second, so 3 ** 4 evaluates to 81. It also supports fractional exponents, such as 4 ** (1 / 2), which evaluates to 2.
Follow-up: How does 2 ** 3 ** 2 group? It is 2 ** (3 ** 2) because exponentiation is right-associative, so the result is 512.
Exponentiation is right-associative. Therefore, a ** b ** c is parsed as a ** (b ** c), rather than (a ** b) ** c.
Follow-up: How can left grouping be requested? Use parentheses, as in (2 ** 3) ** 2, which evaluates to 64 instead of 512.
** operate on BigInts?The operator supports both Number and BigInt exponentiation, but both operands must resolve to the same numeric type. Mixing a BigInt with a Number throws a TypeError.
Follow-up: What happens when numeric strings are used? Operands are coerced to numeric values before JavaScript selects Number or BigInt exponentiation; incompatible resulting types still fail.
Math.pow()Exponentiation assignment (**=)Operator precedence