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 simply 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.
//For arrays
var arr = [
1,
2,
3,
];
arr; // [1, 2, 3]
arr.length; // 3
//For objects
var object = {
foo: "bar",
baz: "qwerty",
age: 42,
};
Trailing comma is allowed in an array, object and function parameters.
Now, this isn’t huge but it’s nice if in case we forget to close off or
rather end a comma if we’re listing a bunch of items it doesn’t matter
if we include that final extra one.
var list = [
"one",
"two",
"three", // It is valid
];
var obj = {
one: "1",
two: "2",
three: "3", // It is valid
}
function add(
one,
two,
three, // It is valid
) {}
const array = [24, 34, 35, 24, , , , 45];
console.log(array);
console.log(array.length)
// Response: [ 24, 34, 35, 24, <3 empty items>, 45 ]
// 8