no-sparse-arrays

Disallow array literals with holes

error by default

Two commas in a row leave a hole in the array. ["red",, "blue"] has a length of 3 and nothing at index 1. forEach, map and filter skip the hole, so the array behaves differently from one that holds undefined there. Most of the time the extra comma is a typo.

A trailing comma doesn't make a hole, so it isn't reported. Skipping an element in destructuring isn't reported either.

This rule is on by default.

Configure#

On by default as an error. To turn it off:

bunfig.toml
[lint.rules]
"no-sparse-arrays" = "off" # or "warn"

Examples#

example.js1 problem
const colors = ["red",, "blue"];
example.jsNo problems
const colors = ["red", "blue"];
const sizes = ["small", undefined, "large"];
const shapes = [
  "circle",
  "square",
];
const [, second] = colors;