7 The comma operator includes multiple expressions where only one is expected. It evaluates each operand from left to right and returns the value of the last operand. However, this frequently obscures side effects, and its use is often an accident. Here are some examples of sequences:
10 var a = (3, 5); // a = 5
14 while (a = next(), a && a.length);
16 (0, eval)("doSomething();");
21 This rule forbids the use of the comma operator, with the following exceptions:
23 * In the initialization or update portions of a `for` statement.
24 * By default, if the expression sequence is explicitly wrapped in parentheses. This exception can be removed with the `allowInParentheses` option.
26 Examples of **incorrect** code for this rule:
31 /*eslint no-sequences: "error"*/
33 foo = doSomething(), val;
35 0, eval("doSomething();");
37 do {} while (doSomething(), !!test);
39 for (; doSomething(), !!test; );
41 if (doSomething(), !!test);
43 switch (val = foo(), val) {}
45 while (val = foo(), val < 42);
47 with (doSomething(), val) {}
52 Examples of **correct** code for this rule:
57 /*eslint no-sequences: "error"*/
59 foo = (doSomething(), val);
61 (0, eval)("doSomething();");
63 do {} while ((doSomething(), !!test));
65 for (i = 0, j = 10; i < j; i++, j--);
67 if ((doSomething(), !!test));
69 switch ((val = foo(), val)) {}
71 while ((val = foo(), val < 42));
73 with ((doSomething(), val)) {}
78 ### Note about arrow function bodies
80 If an arrow function body is a statement rather than a block, and that statement contains a sequence, you need to use double parentheses around the statement to indicate that the sequence is intentional.
82 Examples of **incorrect** code for arrow functions:
87 /*eslint no-sequences: "error"*/
88 const foo = (val) => (console.log('bar'), val);
90 const foo = () => ((bar = 123), 10);
92 const foo = () => { return (bar = 123), 10 }
97 Examples of **correct** code for arrow functions:
102 /*eslint no-sequences: "error"*/
103 const foo = (val) => ((console.log('bar'), val));
105 const foo = () => (((bar = 123), 10));
107 const foo = () => { return ((bar = 123), 10) }
114 This rule takes one option, an object, with the following properties:
116 * `"allowInParentheses"`: If set to `true` (default), this rule allows expression sequences that are explicitly wrapped in parentheses.
118 ### allowInParentheses
120 Examples of **incorrect** code for this rule with the `{ "allowInParentheses": false }` option:
125 /*eslint no-sequences: ["error", { "allowInParentheses": false }]*/
127 foo = (doSomething(), val);
129 (0, eval)("doSomething();");
131 do {} while ((doSomething(), !!test));
133 for (; (doSomething(), !!test); );
135 if ((doSomething(), !!test));
137 switch ((val = foo(), val)) {}
139 while ((val = foo(), val < 42));
141 with ((doSomething(), val)) {}
143 const foo = (val) => ((console.log('bar'), val));
148 Examples of **correct** code for this rule with the `{ "allowInParentheses": false }` option:
153 /*eslint no-sequences: ["error", { "allowInParentheses": false }]*/
155 for (i = 0, j = 10; i < j; i++, j--);
160 ## When Not To Use It
162 Disable this rule if sequence expressions with the comma operator are acceptable.
163 Another case is where you might want to report all usages of the comma operator, even in a for loop. You can achieve this using rule `no-restricted-syntax`:
168 "no-restricted-syntax": ["error", "SequenceExpression"]