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

JS Puzzles · Const

Comma in arrays

A trailing comma after the last item in an array literal is allowed and ignored. It keeps multiline array edits consistent, but additional commas create sparse-array holes.

How it works

A trailing comma may appear after the last item in a JavaScript array literal. It does not create another element, and it can make multiline edits easier because each item line has the same format.

Syntax

const array = [element1, element2,];

Key points

  • Array literals support a comma after their final element.
  • A single final comma does not increase the array's length or add an element.
  • In multiline arrays, trailing commas make adding or removing entries simpler and produce cleaner diffs.
  • Multiple final commas create missing array slots rather than additional ordinary elements.

Common mistakes

  • Do not add multiple commas after the final item: the extra comma creates a sparse-array hole.
  • A hole is not the same as an explicit undefined value; common iteration methods skip it.

Example: Trailing comma in an array

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

console.log(fruits.length); // 3
console.log(fruits[2]); // "Plum"

The final comma is ignored, so the array contains three elements.

When to use it

Keep item lines uniform

Formatting multiline arrays

Make additions produce smaller diffs

Maintaining source code

Distinguish one trailing comma from extra commas

Avoiding accidental sparse arrays

  • Array literals
  • Sparse arrays
  • Array.prototype.forEach()
  • Array.prototype.map()

Source: Arrays. Checked 2026-10-15.