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
|
---
title: 逗號運算子
slug: Web/JavaScript/Reference/Operators/Comma_Operator
translation_of: Web/JavaScript/Reference/Operators/Comma_Operator
---
<div>{{jsSidebar("Operators")}}</div>
<p>The<strong> comma operator</strong> evaluates each of its operands (from left to right) and returns the value of the last operand.</p>
<h2 id="語法">語法</h2>
<pre class="syntaxbox"><em>expr1</em>, <em>expr2, expr3...</em></pre>
<h2 id="參數">參數</h2>
<dl>
<dt><code>expr1</code>, <code>expr2, expr3...</code></dt>
<dd>Any expressions.</dd>
</dl>
<h2 id="描述">描述</h2>
<p>You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a <code>for</code> loop.</p>
<h2 id="範例">範例</h2>
<p>If <code>a</code> is a 2-dimensional array with 10 elements on each side, the following code uses the comma operator to increment two variables at once.</p>
<p>The following code prints the values of the diagonal elements in the array:</p>
<pre class="brush:js">for (var i = 0, j = 9; i <= 9; i++, j--)
console.log("a[" + i + "][" + j + "] = " + a[i][j]);</pre>
<p>Note that the comma in assignments such as the <code>var</code> statement may appear not to have the normal effect of comma operators because they don't exist within an expression. In the following example, <code>a</code> is set to the value of <code>b = 3</code> (which is 3), but the <code>c = 4</code> expression still evaluates and its result returned to console (i.e., 4). This is due to <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence">operator precedence and associativity</a>.</p>
<pre class="brush: js">// Note that the following creates globals and is disallowed in strict mode.
a = b = 3, c = 4; // Returns 4 in console
console.log(a); // 3 (left-most)
x = (y = 5, z = 6); // Returns 6 in console
console.log(x); // 6 (right-most)
</pre>
<p>The comma operator is fully different from the comma within arrays, objects, and function arguments and parameters.</p>
<h3 id="Processing_and_then_returning">Processing and then returning</h3>
<p>Another example that one could make with comma operator is processing before returning. As stated, only the last element will be returned but all others are going to be evaluated as well. So, one could do:</p>
<pre class="brush: js">function myFunc () {
var x = 0;
return (x += 1, x); // the same as return ++x;
}</pre>
<h2 id="規範">規範</h2>
{{Specifications}}
<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
{{Compat}}
<h2 id="參見">參見</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for loop</a></li>
</ul>
|