Skip to content

eslint/no-sparse-arrays Correctness ​

βœ… This rule is turned on by default.

What it does ​

Disallow sparse arrays.

Why is this bad? ​

Take the following example:

javascript
const items = [, ,];

While the items array in this example has a length of 2, there are actually no values in items[0] or items[1]. The fact that the array literal is valid with only commas inside, coupled with the length being set and actual item values not being set, make sparse arrays confusing for many developers.

The confusion around sparse arrays is enough that it’s recommended to avoid using them unless you are certain that they are useful in your code.

Examples ​

Examples of incorrect code for this rule:

javascript
var items = [, ,];
javascript
var colors = ["red", , "blue"];

Examples of correct code for this rule:

javascript
var items = [];

// trailing comma (after the last element) is not a problem

javascript
var colors = ["red", "blue"];

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny no-sparse-arrays
json
{
  "rules": {
    "no-sparse-arrays": "error"
  }
}

References ​

Released under the MIT License.