Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Exponentiation (**)

BaselineWidely available

Theexponentiation (**) operator returns the result of raising the first operand to the power of the second operand. It is equivalent toMath.pow(), except it also acceptsBigInts as operands.

Try it

console.log(3 ** 4);// Expected output: 81console.log(10 ** -2);// Expected output: 0.01console.log(2 ** (3 ** 2));// Expected output: 512console.log((2 ** 3) ** 2);// Expected output: 64

Syntax

js
x ** y

Description

The** operator is overloaded for two types of operands: number andBigInt. It firstcoerces both operands to numeric values and tests the types of them. It performs BigInt exponentiation if both operands become BigInts; otherwise, it performs number exponentiation. ATypeError 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 returns0, and0 raised to a power of0 returns1. For numbers,0 raised to a negative number returnsInfinity, while-0 raised to a negative number returns-Infinity.

NaN ** 0 (and the equivalentMath.pow(NaN, 0)) is the only case whereNaN doesn't propagate through mathematical operations — it returns1 despite the operand beingNaN. In addition, the behavior wherebase is 1 andexponent is non-finite (±Infinity orNaN) is different from IEEE 754, which specifies that the result should be 1, whereas JavaScript returnsNaN to preserve backward compatibility with its original behavior.

For BigInt exponentiation, aRangeError is thrown if the exponenty is negative. This is because any negative exponent would likely result in a value between 0 and 1 (unless the base is1,-1, or0), which is rounded to zero, and is likely a developer mistake.

The exponentiation operator isright-associative:a ** b ** c is equal toa ** (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.

In JavaScript, it is impossible to write an ambiguous exponentiation expression. That is, you cannot put a unary operator (withprecedence 14, including+/-/~/!/++/--/delete/void/typeof/await) immediately before the base number;doing so will cause a SyntaxError.

For example,-2 ** 2 is 4 in Bash, but is -4 in other languages (such as Python). This is invalid in JavaScript, as the operation is ambiguous. You have to parenthesize either side — for example, as-(2 ** 2) — to make the intention unambiguous.

Note that some programming languages use the caret symbol^ for exponentiation, but JavaScript uses that symbol for thebitwise XOR operator.

Examples

Exponentiation using numbers

js
2 ** 3; // 83 ** 2; // 93 ** 2.5; // 15.58845726811989610 ** -1; // 0.12 ** 1024; // InfinityNaN ** 2; // NaNNaN ** 0; // 11 ** Infinity; // NaN

Other non-BigInt values are coerced to numbers:

js
2 ** "3"; // 82 ** "hello"; // NaN

Exponentiation using BigInts

js
2n ** 3n; // 8n2n ** 1024n; // A very large number, but not Infinity

You cannot mix BigInt and number operands in exponentiation.

js
2n ** 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions2 ** 2n; // TypeError: Cannot mix BigInt and other types, use explicit conversions

To do exponentiation with a BigInt and a non-BigInt, convert either operand:

js
2n ** BigInt(2); // 4nNumber(2n) ** 2; // 4

Associativity

js
2 ** 3 ** 2; // 5122 ** (3 ** 2); // 512(2 ** 3) ** 2; // 64

Usage with unary operators

To invert the sign of the result of an exponentiation expression:

js
-(2 ** 2); // -4

To force the base of an exponentiation expression to be a negative number:

js
(-2) ** 2; // 4

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-exp-operator

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp