A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://eslint.org/docs/rules/no-array-constructor below:

no-array-constructor - ESLint - Pluggable JavaScript Linter

no-array-constructor

Disallow Array constructors

🔧 Fixable

Some problems reported by this rule are automatically fixable by the --fix command line option

💡 hasSuggestions

Some problems reported by this rule are manually fixable by editor suggestions

Table of Contents

Use of the Array constructor to construct a new array is generally discouraged in favor of array literal notation because of the single-argument pitfall and because the Array global may be redefined. The exception is when the Array constructor is used to intentionally create sparse arrays of a specified size by giving the constructor a single numeric argument.

Rule Details

This rule disallows Array constructors.

Examples of incorrect code for this rule:

Open in Playground


Array();

Array(0, 1, 2);

new Array(0, 1, 2);

Array(...args);

1
2
3
4
5
6
7
8
9

Examples of correct code for this rule:

Open in Playground


Array(500);

new Array(someOtherArray.length);

[0, 1, 2];

const createArray = Array => new Array();

1
2
3
4
5
6
7
8
9

This rule additionally supports TypeScript type syntax.

Examples of correct code for this rule:

Open in Playground


new Array<number>(1, 2, 3);

new Array<Foo>();

Array<number>(1, 2, 3);

Array<Foo>();

Array?.foo();

1
2
3
4
5
6
7
8
9
10
11

Examples of incorrect code for this rule:

Open in Playground


new Array();

new Array(0, 1, 2);

Array?.(x, y);

Array?.(0, 1, 2);

1
2
3
4
5
6
7
8
9

When Not To Use It

This rule enforces a nearly universal stylistic concern. That being said, this rule may be disabled if the constructor style is preferred.

Version

This rule was introduced in ESLint v0.4.0.

Resources

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4