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.
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.
[
"foo",
+ "baz",
"bar",
- "baz",
]
JavaScript ignores trailing commas in array literals:
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.
const arr = [1, 2, 3, , ,]; arr.length; // 5
Trailing commas in object literals are legal as well:
const object = {
foo: "bar",
baz: "qwerty",
age: 42,
};
Trailing commas are also allowed in function parameter lists.
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.
function f(p) {}
function f(p,) {}
(p) => {};
(p,) => {};
The trailing comma also works with method definitions for classes or objects:
class C {
one(a,) {}
two(a, b,) {}
}
const obj = {
one(a,) {},
two(a, b,) {},
};
The following function invocation pairs are legal and equivalent to each other.
f(p); f(p,); Math.max(10, 20); Math.max(10, 20,);
Function parameter definitions or function invocations only containing a comma will throw a SyntaxError. Furthermore, when using rest parameters, trailing commas are not allowed:
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 ','
A trailing comma is also allowed within a destructuring pattern:
// 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
const [a, ...b,] = [1, 2, 3]; // SyntaxError: rest element may not have a trailing comma
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:
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:
JSON.parse("[1, 2, 3, 4 ]");
JSON.parse('{"foo" : 1 }');
Trailing commas are valid in named imports and named exports.
import {
A,
B,
C,
} from "D";
import { X, Y, Z, } from "W";
import { A as B, C as D, E as F, } from "Z";
export {
A,
B,
C,
};
export { A, B, C, };
export { A as B, C as D, E as F, };
Trailing commas are only allowed in dynamic imports if the runtime also implements the second options parameter.
import("D",);
import(
"D",
{ with: { type: "json" } },
);
Note: The trailing comma in a quantifier actually changes its semantics from matching "exactly n" to matching "at least n".
/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?/
| 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 |
1 | 12 | 1 | 9.5 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 | 1.0.0 | 1.0 | 0.10.0 |
trailing_commas_in_dynamic_import |
91 | 91 | 138 | No | 15 | 91 | 138 | No | 15 | 16.0 | 91 | 15 | 1.0.0 | 1.17 |
17.5.016.15.0–17.0.016.14.0–16.15.0The second parameter no longer throws a parser error, but the--experimental-json-modules flag is still needed to load JSON modules. |
trailing_commas_in_functions |
58 | 14 | 52 | 45 | 10 | 58 | 52 | 43 | 10 | 7.0 | 58 | 10 | 1.0.0 | 1.0 | 8.0.0 |
trailing_commas_in_object_literals |
1 | 12 | 1 | 9.5 | 3 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 | 1.0.0 | 1.0 | 0.10.0 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas