9 Trailing commas in object literals are valid according to the ECMAScript 5 (and ECMAScript 3!) spec. However, IE8 (when not in IE8 document mode) and below will throw an error when it encounters trailing commas in JavaScript.
18 Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched.
19 Another argument in favor of trailing commas is that it improves the clarity of diffs when an item is added or removed from an object or array:
42 This rule enforces consistent use of trailing commas in object and array literals.
46 This rule has a string option or an object option:
50 "comma-dangle": ["error", "never"],
52 "comma-dangle": ["error", {
62 * `"never"` (default) disallows trailing commas
63 * `"always"` requires trailing commas
64 * `"always-multiline"` requires trailing commas when the last element or property is in a *different* line than the closing `]` or `}` and disallows trailing commas when the last element or property is on the *same* line as the closing `]` or `}`
65 * `"only-multiline"` allows (but does not require) trailing commas when the last element or property is in a *different* line than the closing `]` or `}` and disallows trailing commas when the last element or property is on the *same* line as the closing `]` or `}`
67 You can also use an object option to configure this rule for each type of syntax.
68 Each of the following options can be set to `"never"`, `"always"`, `"always-multiline"`, `"only-multiline"`, or `"ignore"`.
69 The default for each option is `"never"` unless otherwise specified.
71 * `arrays` is for array literals and array patterns of destructuring. (e.g. `let [a,] = [1,];`)
72 * `objects` is for object literals and object patterns of destructuring. (e.g. `let {a,} = {a: 1};`)
73 * `imports` is for import declarations of ES Modules. (e.g. `import {a,} from "foo";`)
74 * `exports` is for export declarations of ES Modules. (e.g. `export {a,};`)
75 * `functions` is for function declarations and function calls. (e.g. `(function(a,){ })(b,);`)
76 * `functions` should only be enabled when linting ECMAScript 2017 or higher.
80 Examples of **incorrect** code for this rule with the default `"never"` option:
85 /*eslint comma-dangle: ["error", "never"]*/
102 Examples of **correct** code for this rule with the default `"never"` option:
107 /*eslint comma-dangle: ["error", "never"]*/
126 Examples of **incorrect** code for this rule with the `"always"` option:
131 /*eslint comma-dangle: ["error", "always"]*/
148 Examples of **correct** code for this rule with the `"always"` option:
153 /*eslint comma-dangle: ["error", "always"]*/
172 Examples of **incorrect** code for this rule with the `"always-multiline"` option:
177 /*eslint comma-dangle: ["error", "always-multiline"]*/
184 var foo = { bar: "baz", qux: "quux", };
204 Examples of **correct** code for this rule with the `"always-multiline"` option:
209 /*eslint comma-dangle: ["error", "always-multiline"]*/
216 var foo = {bar: "baz", qux: "quux"};
237 Examples of **incorrect** code for this rule with the `"only-multiline"` option:
242 /*eslint comma-dangle: ["error", "only-multiline"]*/
244 var foo = { bar: "baz", qux: "quux", };
255 Examples of **correct** code for this rule with the `"only-multiline"` option:
260 /*eslint comma-dangle: ["error", "only-multiline"]*/
272 var foo = {bar: "baz", qux: "quux"};
303 Examples of **incorrect** code for this rule with the `{"functions": "never"}` option:
308 /*eslint comma-dangle: ["error", {"functions": "never"}]*/
310 function foo(a, b,) {
319 Examples of **correct** code for this rule with the `{"functions": "never"}` option:
324 /*eslint comma-dangle: ["error", {"functions": "never"}]*/
335 Examples of **incorrect** code for this rule with the `{"functions": "always"}` option:
340 /*eslint comma-dangle: ["error", {"functions": "always"}]*/
351 Examples of **correct** code for this rule with the `{"functions": "always"}` option:
356 /*eslint comma-dangle: ["error", {"functions": "always"}]*/
358 function foo(a, b,) {
367 ## When Not To Use It
369 You can turn this rule off if you are not concerned with dangling commas.