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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
---
title: 運算式與運算子
slug: Web/JavaScript/Reference/Operators
tags:
- NeedsTranslation
- TopicStub
- 運算子
translation_of: Web/JavaScript/Reference/Operators
---
<div>{{jsSidebar("Operators")}}</div>
<p>本章節記錄了所有 JavaScript 運算式、運算子以及關鍵字。</p>
<h2 id="運算式與運算子分類">運算式與運算子分類</h2>
<p>以字母排列的清單請參考左側的側邊欄。</p>
<h3 id="主要運算式">主要運算式</h3>
<p>在 JavaScript 中基本的關鍵字與一般的運算式。</p>
<dl>
<dt>{{jsxref("Operators/this", "this")}}</dt>
<dd><code>this</code> 關鍵字可以參考執行函數的所在位置。</dd>
<dt>{{jsxref("Operators/function", "function")}}</dt>
<dd><code>function</code> 關鍵字可以定義一個函數運算式。</dd>
<dt>{{jsxref("Operators/class", "class")}}</dt>
<dd><code>class</code> 關鍵字可以定義一個類別運算式。</dd>
<dt>{{jsxref("Operators/function*", "function*")}}</dt>
<dd><code>function*</code> 關鍵字可以定義一個 Generator 函數運算式</dd>
<dt>{{jsxref("Operators/yield", "yield")}}</dt>
<dd>暫停與繼續一個產生器 (Generator) 函數。</dd>
<dt>{{jsxref("Operators/yield*", "yield*")}}</dt>
<dd>轉交另一個產生器 (Generator) 函數或可迭代 (Iterable) 的物件。</dd>
<dt>{{experimental_inline}} {{jsxref("Operators/async_function", "async function*")}}</dt>
<dd><code>async</code> 函數可以定義一個非同步函數運算式。</dd>
<dt>{{experimental_inline}} {{jsxref("Operators/await", "await")}}</dt>
<dd>暫停與繼續一個非同步函數並等候承諾的結果/拒絕。</dd>
<dt>{{jsxref("Global_Objects/Array", "[]")}}</dt>
<dd>陣列初始化/書寫格式。</dd>
<dt>{{jsxref("Operators/Object_initializer", "{}")}}</dt>
<dd>物件初始化/書寫格式。</dd>
<dt>{{jsxref("Global_Objects/RegExp", "/ab+c/i")}}</dt>
<dd>正規表示法書寫格式。</dd>
<dt>{{jsxref("Operators/Grouping", "( )")}}</dt>
<dd>分組運算子。</dd>
</dl>
<h3 id="左手邊運算式">左手邊運算式</h3>
<p>左側值為賦值的目標。</p>
<dl>
<dt>{{jsxref("Operators/Property_accessors", "Property accessors", "", 1)}}</dt>
<dd>成員運算子可存取物件的屬性或方法<br>
(<code>object.property</code> and <code>object["property"]</code>)。</dd>
<dt>{{jsxref("Operators/new", "new")}}</dt>
<dd><code>new</code> 運算子可以建立一個建構子 (Constructor) 的實例。</dd>
<dt><a href="/zh-TW/docs/Web/JavaScript/Reference/Operators/new.target">new.target</a></dt>
<dd>在建構子中 <code>new.target</code> 可以參考被 {{jsxref("Operators/new", "new")}} 呼叫的建構子 (Constructor) 。</dd>
<dt>{{jsxref("Operators/super", "super")}}</dt>
<dd><code>super</code> 關鍵字可以呼叫父建構子 (Constructor) 。</dd>
<dt>{{jsxref("Operators/Spread_operator", "...obj")}}</dt>
<dd>The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected.</dd>
</dl>
<h3 id="遞增與遞減">遞增與遞減</h3>
<p>字尾/字首遞增與字尾/字首遞減運算子。</p>
<dl>
<dt>{{jsxref("Operators/Arithmetic_Operators", "A++", "#Increment")}}</dt>
<dd>字尾遞增運算子。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "A--", "#Decrement")}}</dt>
<dd>字尾遞減運算子。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "++A", "#Increment")}}</dt>
<dd>字首遞增運算子。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "--A", "#Decrement")}}</dt>
<dd>字首遞減運算子。</dd>
</dl>
<h3 id="一元運算子">一元運算子</h3>
<p>一元運算是指只需要使用一個運算元的運算。</p>
<dl>
<dt>{{jsxref("Operators/delete", "delete")}}</dt>
<dd><code>delete</code> 運算子可刪除物件中的屬性。</dd>
<dt>{{jsxref("Operators/void", "void")}}</dt>
<dd><code>void</code> 運算子可放棄一個運算式的回傳值。</dd>
<dt>{{jsxref("Operators/typeof", "typeof")}}</dt>
<dd><code>typeof</code> 運算子可以判斷一個指定物件的型態。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "+", "#Unary_plus")}}</dt>
<dd>一元正運算子可以轉換其運算元成為數值 (Number) 型態。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "-", "#Unary_negation")}}</dt>
<dd>一元負運算子可以轉換其運算元成為數值 (Number) 型態並轉為負值。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", "~", "#Bitwise_NOT")}}</dt>
<dd>元位 NOT 運算子。</dd>
<dt>{{jsxref("Operators/Logical_Operators", "!", "#Logical_NOT")}}</dt>
<dd>邏輯 NOT 運算子。</dd>
</dl>
<h3 id="算術運算子">算術運算子</h3>
<p>算術運算子會要用到數值 (不論是字面值或者變數) 做為運算元,並且會回傳單一數值結果。</p>
<dl>
<dt>{{jsxref("Operators/Arithmetic_Operators", "+", "#Addition")}}</dt>
<dd>加法運算子。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "-", "#Subtraction")}}</dt>
<dd>減法運算子。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "/", "#Division")}}</dt>
<dd>除法運算子。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "*", "#Multiplication")}}</dt>
<dd>乘法運算子。</dd>
<dt>{{jsxref("Operators/Arithmetic_Operators", "%", "#Remainder")}}</dt>
<dd>餘數運算子。</dd>
</dl>
<dl>
<dt>{{jsxref("Operators/Arithmetic_Operators", "**", "#Exponentiation")}}</dt>
<dd>指數運算子。</dd>
</dl>
<h3 id="關係運算子">關係運算子</h3>
<p>比較運算子會比較其運算元並根據比較的結果是否成立回傳一個 <code>Boolean</code> 值的結果。</p>
<dl>
<dt>{{jsxref("Operators/in", "in")}}</dt>
<dd><code>in</code> 運算子會判斷一個物件是否有指定的屬性。</dd>
<dt>{{jsxref("Operators/instanceof", "instanceof")}}</dt>
<dd><code>instanceof</code> 運算子會判斷一個物件是否為另一個物件的實例。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "<", "#Less_than_operator")}}</dt>
<dd>小於運算子。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", ">", "#Greater_than_operator")}}</dt>
<dd>大於運算子。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "<=", "#Less_than_or_equal_operator")}}</dt>
<dd>小於等於運算子。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", ">=", "#Greater_than_or_equal_operator")}}</dt>
<dd>大於等於運算子。</dd>
</dl>
<div class="note">
<p><strong>Note: =></strong> is not an operator, but the notation for <a href="/zh-TW/docs/Web/JavaScript/Reference/Functions/Arrow_functions">Arrow functions</a>.</p>
</div>
<h3 id="相等運算子">相等運算子</h3>
<p>執行相對運算子後的結果為比較結果是否成立的 <code>Boolean</code> 值。</p>
<dl>
<dt>{{jsxref("Operators/Comparison_Operators", "==", "#Equality")}}</dt>
<dd>相等運算子。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "!=", "#Inequality")}}</dt>
<dd>不相等運算子。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "===", "#Identity")}}</dt>
<dd>恆等運算子。</dd>
<dt>{{jsxref("Operators/Comparison_Operators", "!==", "#Nonidentity")}}</dt>
<dd>不恆等運算子。</dd>
</dl>
<h3 id="位元移位運算子">位元移位運算子</h3>
<p>用來位移運算元所有位元的運算。</p>
<dl>
<dt>{{jsxref("Operators/Bitwise_Operators", "<<", "#Left_shift")}}</dt>
<dd>左移位元運算子。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", ">>", "#Right_shift")}}</dt>
<dd>右移位元運算子。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", ">>>", "#Unsigned_right_shift")}}</dt>
<dd>右移無號位元運算子。</dd>
</dl>
<h3 id="二元位元運算子">二元位元運算子</h3>
<p>位元運算子將其運算元當做是 32 位元 (32 個零與一) 的數值處理並回傳標準 JavaScript 數值。</p>
<dl>
<dt>{{jsxref("Operators/Bitwise_Operators", "&", "#Bitwise_AND")}}</dt>
<dd>位元 AND。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", "|", "#Bitwise_OR")}}</dt>
<dd>位元 OR。</dd>
<dt>{{jsxref("Operators/Bitwise_Operators", "^", "#Bitwise_XOR")}}</dt>
<dd>位元 XOR。</dd>
</dl>
<h3 id="二元邏輯運算子">二元邏輯運算子</h3>
<p>邏輯運算子一般用在處理布林 (邏輯) 值,它們存在的地方會回傳一個布林 (Boolean) 值。</p>
<dl>
<dt>{{jsxref("Operators/Logical_Operators", "&&", "#Logical_AND")}}</dt>
<dd>邏輯 AND。</dd>
<dt>{{jsxref("Operators/Logical_Operators", "||", "#Logical_OR")}}</dt>
<dd>邏輯 OR。</dd>
</dl>
<h3 id="條件_(三元)_運算子">條件 (三元) 運算子</h3>
<dl>
<dt>{{jsxref("Operators/Conditional_Operator", "(condition ? ifTrue : ifFalse)")}}</dt>
<dd>
<p>條件運算子會根據條件的邏輯值判斷並回傳其中一個值。</p>
</dd>
</dl>
<h3 id="賦值運算子">賦值運算子</h3>
<p>賦值運算子會根據其右側運算元的數值處理後賦值給其左側的運算元。</p>
<dl>
<dt>{{jsxref("Operators/Assignment_Operators", "=", "#Assignment")}}</dt>
<dd>賦值運算子。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "*=", "#Multiplication_assignment")}}</dt>
<dd>乘法賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "/=", "#Division_assignment")}}</dt>
<dd>除法賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "%=", "#Remainder_assignment")}}</dt>
<dd>餘數賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "+=", "#Addition_assignment")}}</dt>
<dd>加法賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "-=", "#Subtraction_assignment")}}</dt>
<dd>減法賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "<<=", "#Left_shift_assignment")}}</dt>
<dd>左移賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", ">>=", "#Right_shift_assignment")}}</dt>
<dd>右移賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", ">>>=", "#Unsigned_right_shift_assignment")}}</dt>
<dd>無號右移賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "&=", "#Bitwise_AND_assignment")}}</dt>
<dd>位元 AND 賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "^=", "#Bitwise_XOR_assignment")}}</dt>
<dd>位元 XOR 賦值。</dd>
<dt>{{jsxref("Operators/Assignment_Operators", "|=", "#Bitwise_OR_assignment")}}</dt>
<dd>位元 OR 賦值。</dd>
<dt>{{jsxref("Operators/Destructuring_assignment", "[a, b] = [1, 2]")}}<br>
{{jsxref("Operators/Destructuring_assignment", "{a, b} = {a:1, b:2}")}}</dt>
<dd>
<p>解構賦值讓您可使用如陣列或物件書寫格式來賦值給陣列或物件的屬性。</p>
</dd>
</dl>
<h3 id="逗號運算子">逗號運算子</h3>
<dl>
<dt>{{jsxref("Operators/Comma_Operator", ",")}}</dt>
<dd>逗號運算子允許在一個敘述句中執行多個運算式並回傳最後一個運算式的結果。</dd>
</dl>
<h3 id="非標準功能">非標準功能</h3>
<dl>
<dt>{{non-standard_inline}} {{jsxref("Operators/Legacy_generator_function", "Legacy generator function", "", 1)}}</dt>
<dd>The <code>function</code> keyword can be used to define a legacy generator function inside an expression. To make the function a legacy generator, the function body should contains at least one {{jsxref("Operators/yield", "yield")}} expression.</dd>
<dt>{{non-standard_inline}} {{jsxref("Operators/Expression_closures", "Expression closures", "", 1)}}</dt>
<dd>The expression closure syntax is a shorthand for writing simple function.</dd>
<dt>{{non-standard_inline}} {{jsxref("Operators/Array_comprehensions", "[for (x of y) x]")}}</dt>
<dd>Array comprehensions.</dd>
<dt>{{non-standard_inline}} {{jsxref("Operators/Generator_comprehensions", "(for (x of y) y)")}}</dt>
<dd>Generator comprehensions.</dd>
</dl>
<h2 id="技術規格">技術規格</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">技術規格</th>
<th scope="col">狀態</th>
<th scope="col">備註</th>
</tr>
<tr>
<td>{{SpecName('ES1', '#sec-11', 'Expressions')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-11', 'Expressions')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-ecmascript-language-expressions', 'ECMAScript Language: Expressions')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>New: Spread operator, destructuring assignment, <code>super</code> keyword.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-ecmascript-language-expressions', 'ECMAScript Language: Expressions')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="相關文獻">相關文獻</h2>
<ul>
<li><a href="/zh-TW/docs/Web/JavaScript/Reference/Operators/Operator_Precedence">Operator precedence</a></li>
</ul>
|