[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Trailing_commas [Back]  [Original]

Trailing commas - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Trailing commas

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

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

Trailing commas ("final commas" ) , JavaScript . , trailing comma . .

JavaScript trailing comma , ECMAScript 5 , ECMAScript 2017 .

JSON trailing comma .

In this article

trailing comma

JavaScript trailing comma .

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

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

trailing comma ("") . (sparse) ((dense) ). Array.prototype.forEach() Array.prototype.map() .

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

ECMAScript 5 trailing comma .

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

trailing comma

ECMAScript 2017 trailing comma .

, . Trailing comma length arguments .

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

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

Trailing comma .

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

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

, .

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

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

trailing comma

SyntaxError . , rest trailing comma .

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 comma

trailing comma .

js
// Trailing comma    
[a, b,] = [1, 2];

// Trailing comma    
var o = {
  p: 42,
  q: true,
};
var {p, q,} = o;

Rest SyntaxError .

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

JSON trailing comma

trailing comma ECMAScript 5 . JSON ES5 JavaScript JSON trailing comma .

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

JSON trailing comma .

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

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


Web Proxy Viewer  |  New URL  |  Original Page