Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array() constructor

BaselineWidely available

TheArray() constructor createsArray objects.

Syntax

js
new Array()new Array(element1)new Array(element1, element2)new Array(element1, element2, /* …, */ elementN)new Array(arrayLength)Array()Array(element1)Array(element1, element2)Array(element1, element2, /* …, */ elementN)Array(arrayLength)

Note:Array() can be called with or withoutnew. Both create a newArray instance.

Parameters

element1, …,elementN

A JavaScript array is initialized with the given elements, except in the case wherea single argument is passed to theArray constructor and that argument isa number (see thearrayLength parameter below). Note that this special case onlyapplies to JavaScript arrays created with theArray constructor, notarray literals created with the square bracket syntax.

arrayLength

If the only argument passed to theArray constructor is an integerbetween 0 and 232 - 1 (inclusive), this returns a new JavaScript array withitslength property set to that number.

Note:This implies an array ofarrayLength empty slots, not slots with actualundefined values — seesparse arrays).

Exceptions

RangeError

Thrown if there's only one argument (arrayLength) that is a number, but its value is not an integer or not between 0 and 232 - 1 (inclusive).

Examples

Array literal notation

Arrays can be created using theliteralnotation:

js
const fruits = ["Apple", "Banana"];console.log(fruits.length); // 2console.log(fruits[0]); // "Apple"

Array constructor with a single parameter

Arrays can be created using a constructor with a single number parameter. An array is created withitslength property set to that number, and the array elements are emptyslots.

js
const arrayEmpty = new Array(2);console.log(arrayEmpty.length); // 2console.log(arrayEmpty[0]); // undefined; actually, it is an empty slotconsole.log(0 in arrayEmpty); // falseconsole.log(1 in arrayEmpty); // false
js
const arrayOfOne = new Array("2"); // Not the number 2 but the string "2"console.log(arrayOfOne.length); // 1console.log(arrayOfOne[0]); // "2"

Array constructor with multiple parameters

If more than one argument is passed to the constructor, a newArray withthe given elements is created.

js
const fruits = new Array("Apple", "Banana");console.log(fruits.length); // 2console.log(fruits[0]); // "Apple"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-array-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp