blob: 7e138abfa2024e451f784dc8f528e1d8a287ac2d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
---
title: 求幂 (**)
slug: Web/JavaScript/Reference/Operators/Exponentiation
tags:
- JavaScript
- 参考
- 语言特征
- 运算符
translation_of: Web/JavaScript/Reference/Operators/Exponentiation
---
<div>{{jsSidebar("Operators")}}</div>
<p>求幂运算符(<code>**</code>)返回将第一个操作数加到第二个操作数的幂的结果。它等效于<code>Math.pow</code>,不同之处在于它也接受BigInts作为操作数。</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-exponentiation.html")}}</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><strong>Operator:</strong> <var>var1</var> ** <var>var2</var>
</pre>
<h2 id="简介">简介</h2>
<p>求幂运算符是是<strong>右结合的</strong>: <code><var>a</var> ** <var>b</var> ** <var>c</var></code> 等于 <code><var>a</var> ** (<var>b</var> ** <var>c</var>)</code>.</p>
<p>在大多数语言里,比如PHP、Python等那些有一个幂运算符 (<code>**</code>) 的语言,幂运算符被定义有一个比一元运算符,比如一元的 <code>+</code> 和一元的 <code>-</code> 更高的运算顺序,但有一些例外。在Bash语言里,<code>**</code> 运算符被定义有一个比一元运算符更低的运算顺序。</p>
<p>在JavaScript里,你不可能写出一个不明确的求幂表达式。这就是说,你不能立刻将一个一元运算符(<code>+/-/~/!/delete/void/typeof</code>)放在基数前,这样做只会导致一个语法错误。</p>
<pre class="brush: js">-2 ** 2;
// 4 in Bash, -4 in other languages.
// This is invalid in JavaScript, as the operation is ambiguous.
-(2 ** 2);
// -4 in JavaScript and the author's intention is unambiguous.
</pre>
<p>注意有些编程语言用扬抑符 <kbd>^</kbd> 做乘方运算,但是JavaScript将这个符号作为了<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR">XOR位逻辑运算符</a>。</p>
<h2 id="例子">例子</h2>
<h3 id="基本求幂">基本求幂</h3>
<pre class="brush: js">2 ** 3 // 8
3 ** 2 // 9
3 ** 2.5 // 15.588457268119896
10 ** -1 // 0.1
NaN ** 2 // NaN
</pre>
<h3 id="结合">结合</h3>
<pre class="brush: js">2 ** 3 ** 2 // 512
2 ** (3 ** 2) // 512
(2 ** 3) ** 2 // 64</pre>
<h3 id="与一元运算符的用法">与一元运算符的用法</h3>
<p>取求幂表达式的值的相反数:</p>
<pre class="brush: js">-(2 ** 2) // -4
</pre>
<p>将求幂表达式的底数转化为一个负数:</p>
<pre class="brush: js">(-2) ** 2 // 4
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-exp-operator', 'Exponentiation operator')}}</td>
</tr>
</tbody>
</table>
<h2 id="浏览器支持">浏览器支持</h2>
<p>{{Compat("javascript.operators.exponentiation")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition">Addition operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Subtraction">Subtraction operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Division">Division operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Multiplication">Multiplication operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder">Remainder operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment">Increment operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Decrement">Decrement operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_negation">Unary negation operator</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus">Unary plus operator</a></li>
</ul>
<p>
<audio class="hidden"></audio>
</p>
|