Skip to content

Files

Latest commit

 

History

History
24 lines (18 loc) · 650 Bytes

no-array-constructor.md

File metadata and controls

24 lines (18 loc) · 650 Bytes

Pattern: Creation of array using Array constructor

Issue: -

Description

Using the Array constructor can lead to unexpected behavior due to its single-argument pitfall and potential global redefinition. Array literal notation is preferred except when creating sparse arrays of a specific size.

Examples

Example of incorrect code:

let arr = new Array();
let numbers = new Array(1, 2, 3);
let items = new Array('item');

Example of correct code:

let arr = [];
let numbers = [1, 2, 3];
let sparseArr = new Array(10);  // Creating sparse array with specific size
let items = Array.from(iterable);