Skip to content

noSparseArray

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noSparseArray": "error"
}
}
}
}

Prevents the use of sparse arrays (arrays with holes).

Sparse arrays may contain empty slots due to the use of multiple commas between two items, like the following:

const items = [a,,b];

Arrays with holes might yield incorrect information. For example, the previous snippet, items has a length of 4, but did the user really intended to have an array with four items? Or was it a typo.

This rule enforce the user to explicitly an undefined in places where there’s a hole.

[1,,2]
code-block.js:1:1 lint/suspicious/noSparseArray  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This array contains an empty slots..

> 1 │ [1,,2]
^^^^^^
2 │

The presences of empty slots may cause incorrect information and might be a typo.

Unsafe fix: Replace hole with undefined

1 │ [1,·undefined,2]
++++++++++
[1, undefined, 2]