求幂 (**)

求幂 (**) 运算符返回第一个操作数的第二个操作数次幂的结果。它与 Math.pow() 等效,但它也接受 BigInts 作为操作数。

¥The exponentiation (**) operator returns the result of raising the first operand to the power of the second operand. It is equivalent to Math.pow(), except it also accepts BigInts as operands.

Try it

语法

¥Syntax

js
x ** y

描述

¥Description

** 运算符针对两种类型的操作数进行重载:编号和 BigInt。它首先 将两个操作数强制转换为数值 并测试它们的类型。如果两个操作数都变为 BigInt,则执行 BigInt 求幂;否则,它执行数字求幂。如果一个操作数变为 BigInt 而另一个操作数变为数字,则抛出 TypeError

¥The ** 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 become BigInts; otherwise, it performs number exponentiation. A TypeError is thrown if one operand becomes a BigInt but the other becomes a number.

对于数字和 BigInt,0 的正幂返回 000 幂返回 1。对于数字,0 为负数返回 Infinity,而 -0 为负数则返回 -Infinity

¥For both numbers and BigInts, 0 raised to a positive power returns 0, and 0 raised to a power of 0 returns 1. For numbers, 0 raised to a negative number returns Infinity, while -0 raised to a negative number returns -Infinity.

NaN ** 0(以及等效的 Math.pow(NaN, 0))是 NaN 不通过数学运算传播的唯一情况 - 尽管操作数是 NaN,它仍返回 1。此外,base 为 1 且 exponent 为非有限(±Infinity 或 NaN)的行为与 IEEE 754 不同,IEEE 754 指定结果应为 1,而 JavaScript 返回 NaN 以保持与其原始行为的向后兼容性。

¥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) is different from IEEE 754, which specifies that the result should be 1, whereas JavaScript returns NaN to preserve backward compatibility with its original behavior.

对于 BigInt 求幂,如果指数 y 为负,则抛出 RangeError。这是因为任何负指数都可能导致 0 到 1 之间的值(除非基数为 1-10),该值四舍五入为零,并且可能是开发者的错误。

¥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 1, -1, or 0), which is rounded to zero, and is likely a developer mistake.

求幂运算符是 right-associativea ** b ** c 等于 a ** (b ** c)

¥The exponentiation operator is right-associative: a ** b ** c is equal to a ** (b ** c).

在大多数语言中,例如 PHP、Python 和其他具有幂运算符 (**) 的语言,幂运算符被定义为比一元运算符(例如一元 + 和一元 -)具有更高的优先级,但也有一些例外。例如,在 Bash 中,** 运算符被定义为比一元运算符具有更低的优先级。

¥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.

在 JavaScript 中,不可能编写不明确的求幂表达式。也就是说,不能在基数之前放置一元运算符(优先级 14,包括 +/-/~/!/++/--/delete/void/typeof/await);这样做会导致语法错误

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

例如,-2 ** 2 在 Bash 中为 4,但在其他语言(例如 Python)中为 -4。这在 JavaScript 中是无效的,因为该操作是不明确的。你必须将任一侧括起来 - 例如,作为 -(2 ** 2) - 以使意图明确。

¥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.

请注意,某些编程语言使用脱字符号 ^ 进行求幂,但 JavaScript 使用该符号表示 按位异或运算符

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

示例

¥Examples

使用数字进行指数赋值

¥Exponentiation using numbers

js
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

其他非 BigInt 值被强制转换为数字:

¥Other non-BigInt values are coerced to numbers:

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

使用 BigInts 进行指数赋值

¥Exponentiation using BigInts

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

你不能在指数运算中混合 BigInt 和数字操作数。

¥You cannot mix BigInt and number operands in exponentiation.

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

要使用 BigInt 和非 BigInt 进行指数运算,请转换任一操作数:

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

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

关联性

¥Associativity

js
2 ** 3 ** 2; // 512
2 ** (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

规范

Specification
ECMAScript Language Specification
# sec-exp-operator

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看