Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Trailing commas

Trailing commas may follow the final item in many JavaScript comma-separated lists. They are optional, preserve semantics, and make multiline edits cleaner, but JSON and rest parameters have different rules.

How it works

A trailing comma is an optional comma after the final item in a comma-separated JavaScript list. It does not change the meaning of supported arrays, objects, parameters, calls, imports, exports, or destructuring, and it makes multiline edits easier; JSON does not permit it.

Syntax

const array = [item,];
const object = { key: value, };
function fn(parameter,) {}

Key points

  • Arrays may end with an optional trailing comma.
  • Object literals may place a comma after their final property.
  • Trailing commas are supported in several comma-separated JavaScript constructs.
  • Removing or adding a supported trailing comma does not alter program behavior.
  • In multiline lists, this style simplifies adding, removing, and rearranging entries.

Common mistakes

  • A rest parameter must be the final parameter, so a comma after it is a syntax error.
  • Do not use trailing commas in JSON, even though the corresponding JavaScript array or object may allow them.
  • An extra comma in an array can create a sparse-array hole rather than merely adding another harmless trailing comma.

Example: Trailing commas in common JavaScript lists

const fruits = [
  "Apple",
  "Orange",
  "Plum",
];

const user = {
  name: "John",
  age: 30,
};

function greet(name,) {
  return `Hello, ${name}`;
}

console.log(greet("Ada"));

This runnable example uses optional trailing commas in an array, object, and function parameter list.

When to use it

Adding or removing array items

Maintaining multiline arrays

Editing object properties

Maintaining multiline objects

Keeping parameter diffs focused

Declaring functions with multiline parameters

Using a uniform final-comma style

Formatting JavaScript source consistently

  • Array literals
  • Object literals
  • Function parameter lists
  • Function calls
  • Destructuring
  • Named imports
  • Named exports

Source: Arrays. Checked 2026-10-15.