The Array()
constructor is used to create Array
objects.
[element0, element1, ..., elementN] new Array(element0, element1[, ...[, elementN]]) new Array(arrayLength)
elementN
Array
constructor and that argument is a number (see the arrayLength parameter below). Note that this special case only applies to JavaScript arrays created with the Array
constructor, not array literals created with the bracket syntax.arrayLength
Array
constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with its length
property set to that number (Note: this implies an array of arrayLength
empty slots, not slots with actual undefined
values). If the argument is any other number, a RangeError
exception is thrown.Arrays can be created using the literal notation:
let fruits = ['Apple', 'Banana']; console.log(fruits.length); // 2 console.log(fruits[0]); // "Apple"
Arrays can be created using a constructor with a single number parameter. An array with its length
property set to that number and the array elements are empty slots.
let fruits = new Array(2); console.log(fruits.length); // 2 console.log(fruits[0]); // undefined
If more than one argument is passed to the constructor, a new Array
with the given elements is created.
let fruits = new Array('Apple', 'Banana'); console.log(fruits.length); // 2 console.log(fruits[0]); // "Apple"
Desktop | ||||||
---|---|---|---|---|---|---|
Array() constructor |
1 | 12 | 1 | 4 | 4 | 1 |
Mobile | ||||||
---|---|---|---|---|---|---|
Array() constructor |
≤37 | 18 | 4 | 10.1 | 1 | 1.0 |
Server | |
---|---|
Array() constructor |
0.1.100 |
Array
class
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Array