Summary:
Allow a trailing comma in function parameter declarations and function call parameters. Example:
function f(a, b,) {
return a + b;
}
let g = (a, b,) => a + b;
f(1, 2,) + g(3, 4,);
The purpose of this feature is to better support writing each parameter on its own line:
function f(
longParameterName1, // documentation for parameter
longParameterName2, // documentation for parameter
longParameterName3, // documentation for parameter
) {
}
f(
complicatedExpression(), // this is longParameterName1
complicatedExpression(), // this is longParameterName2
complicatedExpression(), // this is longParameterName3
);
Allowing the final comma is beneficial when editing the code. If you want to reorder the parameters, you needn't add and remove commas from some of the lines sometimes. If you want to add a new final parameter, you can simply add a line without modifying the previous line. This makes version-control diffs cleaner and also makes editing code less troublesome and error prone. Trailing commas are allowed in array and object literal syntax for the same reasons.
Interoperability and Compatibility Risk:
This new language feature allows syntax that was previously a SyntaxError, so compatibility risk is low.
This feature is in the draft ES2017 spec.
Firefox has implemented (
https://bugzilla.mozilla.org/show_bug.cgi?id=1303788 ) but not yet shipped this feature.
Webkit has implemented (
https://bugs.webkit.org/show_bug.cgi?id=158020 ) but not yet shipped this feature.
Edge is shipping with this feature enabled (
https://kangax.github.io/compat-table/es2016plus/ ).