Trailing commas

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can add a new line without modifying the previously last line if that line already uses a trailing comma. This makes version-control diffs cleaner and editing code might be less troublesome.

JavaScript has allowed trailing commas in array literals since the beginning. Trailing commas are now also allowed in object literals, function parameters, named imports, named exports, and more.

JSON, however, disallows all trailing commas.

Description

JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item. This includes:

In all these cases, the trailing comma is entirely optional and doesn't change the program's semantics in any way.

It is particularly useful when adding, removing, or reordering items in a list that spans multiple lines, because it reduces the number of lines that need to be changed, which helps with both editing and reviewing the diff.

diff
  [
    "foo",
+   "baz",
    "bar",
-   "baz",
  ]

Examples

Trailing commas in literals

Arrays

JavaScript ignores trailing commas in array literals:

js
const arr = [
  1,
  2,
  3,
];

arr; // [1, 2, 3]
arr.length; // 3

If more than one trailing comma is used, an elision (or hole) is produced. An array with holes is called sparse (a dense array has no holes). When iterating arrays for example with Array.prototype.forEach() or Array.prototype.map(), array holes are skipped. Sparse arrays are generally unfavorable, so you should avoid having multiple trailing commas.

js
const arr = [1, 2, 3, , ,];
arr.length; // 5

Objects

Trailing commas in object literals are legal as well:

js
const object = {
  foo: "bar",
  baz: "qwerty",
  age: 42,
};

Trailing commas in functions

Trailing commas are also allowed in function parameter lists.

Parameter definitions

The following function definition pairs are legal and equivalent to each other. Trailing commas don't affect the length property of function declarations or their arguments object.

js
function f(p) {}
function f(p,) {}

(p) => {};
(p,) => {};

The trailing comma also works with method definitions for classes or objects:

js
class C {
  one(a,) {}
  two(a, b,) {}
}

const obj = {
  one(a,) {},
  two(a, b,) {},
};

Function calls

The following function invocation pairs are legal and equivalent to each other.

js
f(p);
f(p,);

Math.max(10, 20);
Math.max(10, 20,);

Illegal trailing commas

Function parameter definitions or function invocations only containing a comma will throw a SyntaxError. Furthermore, when using rest parameters, trailing commas are not allowed:

js
function f(,) {} // SyntaxError: missing formal parameter
(,) => {};       // SyntaxError: expected expression, got ','
f(,)             // SyntaxError: expected expression, got ','

function f(...p,) {} // SyntaxError: parameter after rest parameter
(...p,) => {}        // SyntaxError: expected closing parenthesis, got ','

Trailing commas in destructuring

A trailing comma is also allowed within a destructuring pattern:

js
// array destructuring with trailing comma
[a, b,] = [1, 2];

// object destructuring with trailing comma
const o = {
  p: 42,
  q: true,
};
const { p, q, } = o;

However, a trailing comma is not allowed after the rest element, if present

js
const [a, ...b,] = [1, 2, 3];
// SyntaxError: rest element may not have a trailing comma

Trailing commas in JSON

As JSON is based on a very restricted subset of JavaScript syntax, trailing commas are not allowed in JSON.

Both lines will throw a SyntaxError:

js
JSON.parse("[1, 2, 3, 4, ]");
JSON.parse('{"foo" : 1, }');
// SyntaxError JSON.parse: unexpected character
// at line 1 column 14 of the JSON data

Omit the trailing commas to parse the JSON correctly:

js
JSON.parse("[1, 2, 3, 4 ]");
JSON.parse('{"foo" : 1 }');

Trailing commas in named imports and named exports

Trailing commas are valid in named imports and named exports.

Named imports

js
import {
  A,
  B,
  C,
} from "D";

import { X, Y, Z, } from "W";

import { A as B, C as D, E as F, } from "Z";

Named exports

js
export {
  A,
  B,
  C,
};

export { A, B, C, };

export { A as B, C as D, E as F, };

Trailing commas in dynamic import

Trailing commas are only allowed in dynamic imports if the runtime also implements the second options parameter.

js
import("D",);
import(
  "D",
  { with: { type: "json" } },
);

Quantifier prefix

Note: The trailing comma in a quantifier actually changes its semantics from matching "exactly n" to matching "at least n".

js
/x{2}/; // Exactly 2 occurrences of "x"; equivalent to /xx/
/x{2,}/; // At least 2 occurrences of "x"; equivalent to /xx+/
/x{2,4}/; // 2 to 4 occurrences of "x"; equivalent to /xxx?x?/

Specifications

Specification
ECMAScript® 2027 Language Specification
# prod-Elision
ECMAScript® 2027 Language Specification
# prod-ObjectLiteral
ECMAScript® 2027 Language Specification
# prod-ArrayLiteral
ECMAScript® 2027 Language Specification
# prod-Arguments
ECMAScript® 2027 Language Specification
# prod-FormalParameters
ECMAScript® 2027 Language Specification
# prod-CoverParenthesizedExpressionAndArrowParameterList
ECMAScript® 2027 Language Specification
# prod-NamedImports
ECMAScript® 2027 Language Specification
# prod-NamedExports
ECMAScript® 2027 Language Specification
# prod-QuantifierPrefix
ECMAScript® 2027 Language Specification
# prod-annexB-InvalidBracedQuantifier

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
Trailing commas
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 9.5 (Release date: 2008-06-12)
footnote Full support
Safari – Full support
Safari 1 (Release date: 2003-06-23)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.10 (Release date: 2013-03-11)
footnote Full support
Trailing comma in dynamic import
Chrome – Full support
Chrome 91 (Release date: 2021-05-25)
footnote Full support
Edge – Full support
Edge 91 (Release date: 2021-05-27)
footnote Full support
Firefox – Full support
Firefox 138 (Release date: 2025-04-29)
footnote Full support
Opera – No support
Opera
footnote No support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 91 (Release date: 2021-05-25)
footnote Full support
Firefox for Android – Full support
Firefox for Android 138 (Release date: 2025-04-29)
footnote Full support
Opera Android – No support
Opera Android
footnote No support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 16 (Release date: 2021-11-25)
footnote Full support
WebView Android – Full support
WebView Android 91 (Release date: 2021-05-25)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1.17 (Release date: 2021-12-16)
footnote Full support
Node.js – Partial support
Node.js 16.14 – 16.14 (Release date: 2022-02-08)
footnote Partial support
footnote The second parameter no longer throws a parser error, but the --experimental-json-modules flag is still needed to load JSON modules.
Node.js – No support
Node.js 16.15 – 16.17 (Release date: 2022-04-27)
footnote Removed in 17 and later
Node.js – Full support
Node.js 17.5 (Release date: 2022-02-10)
footnote Full support
Trailing comma in function parameters
Chrome – Full support
Chrome 58 (Release date: 2017-04-19)
footnote Full support
Edge – Full support
Edge 14 (Release date: 2016-08-02)
footnote Full support
Firefox – Full support
Firefox 52 (Release date: 2017-03-07)
footnote Full support
Opera – Full support
Opera 45 (Release date: 2017-05-10)
footnote Full support
Safari – Full support
Safari 10 (Release date: 2016-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 58 (Release date: 2017-04-25)
footnote Full support
Firefox for Android – Full support
Firefox for Android 52 (Release date: 2017-03-07)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 10 (Release date: 2016-09-13)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – Full support
WebView Android 58 (Release date: 2017-04-25)
footnote Full support
WebView on iOS – Full support
WebView on iOS 10 (Release date: 2016-09-13)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 8 (Release date: 2017-05-30)
footnote Full support
Trailing comma in object literals
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 9.5 (Release date: 2008-06-12)
footnote Full support
Safari – Full support
Safari 3 (Release date: 2007-10-26)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote Full support
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 0.10 (Release date: 2013-03-11)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Has more compatibility info.

See also