src/content/docs/linter/rules/no-sparse-array.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/suspicious/noSparseArray`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-sparse-arrays`](https://eslint.org/docs/latest/rules/no-sparse-arrays){
"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]
[1, undefined, 2]