]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-array-constructor.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-array-constructor.md
1 ---
2 title: no-array-constructor
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-new-object
7 - no-new-wrappers
8 ---
9
10
11 Use of the `Array` constructor to construct a new array is generally
12 discouraged in favor of array literal notation because of the single-argument
13 pitfall and because the `Array` global may be redefined. The exception is when
14 the Array constructor is used to intentionally create sparse arrays of a
15 specified size by giving the constructor a single numeric argument.
16
17 ## Rule Details
18
19 This rule disallows `Array` constructors.
20
21 Examples of **incorrect** code for this rule:
22
23 :::incorrect
24
25 ```js
26 /*eslint no-array-constructor: "error"*/
27
28 Array(0, 1, 2)
29
30 new Array(0, 1, 2)
31 ```
32
33 :::
34
35 Examples of **correct** code for this rule:
36
37 :::correct
38
39 ```js
40 /*eslint no-array-constructor: "error"*/
41
42 Array(500)
43
44 new Array(someOtherArray.length)
45
46 [0, 1, 2]
47 ```
48
49 :::
50
51 ## When Not To Use It
52
53 This rule enforces a nearly universal stylistic concern. That being said, this
54 rule may be disabled if the constructor style is preferred.