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