You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/src/rules/no-sparse-arrays.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -10,13 +10,13 @@ further_reading:
10
10
Sparse arrays contain empty slots, most frequently due to multiple commas being used in an array literal, such as:
11
11
12
12
```js
13
-
var items = [,,];
13
+
constitems= [,,];
14
14
```
15
15
16
16
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. Consider the following:
17
17
18
18
```js
19
-
var colors = [ "red",, "blue" ];
19
+
constcolors= [ "red",, "blue" ];
20
20
```
21
21
22
22
In this example, the `colors` array has a `length` of 3. But did the developer intend for there to be an empty spot in the middle of the array? Or is it a typo?
@@ -34,8 +34,8 @@ Examples of **incorrect** code for this rule:
34
34
```js
35
35
/*eslint no-sparse-arrays: "error"*/
36
36
37
-
var items = [,];
38
-
var colors = [ "red",, "blue" ];
37
+
constitems= [,];
38
+
constcolors= [ "red",, "blue" ];
39
39
```
40
40
41
41
:::
@@ -47,11 +47,11 @@ Examples of **correct** code for this rule:
47
47
```js
48
48
/*eslint no-sparse-arrays: "error"*/
49
49
50
-
var items = [];
51
-
var items=newArray(23);
50
+
constitems= [];
51
+
constarr=newArray(23);
52
52
53
53
// trailing comma (after the last element) is not a problem
0 commit comments