Array (Array - JavaScript)

Creates a new Array object.

Defined in

Array (Standard - JavaScript)

Syntax

Array()

Array(values:number)

Array(values:any, ...)

Parameters Description
values number The number of elements in the array. If the constructor specifies a single numeric parameter, the new array contains the specified number of undefined elements.
values any The values of the elements in the array. If the constructor contains parameters other than a single numeric parameter, the new array contains one element for each parameter. A parameter can be any expression.

Literal notation

Literal notation can be used to create an array by specifying [parameters] in place of new Array(parameters). For example, the following are equivalent:
var a = new Array("one", true, 3)
var a = ["one", true, 3]

Multidimensional arrays

Create multidimensional arrays by specifying arrays for array elements. This example creates a two-dimensional array of nine undefined elements:
var a = new Array(new Array(3), new Array(3), new Array(3))

Array element references

Make reference to an array element with a subscript in square brackets. The first element of an array is element 0. For example, if a is an array, a[0] is the first element, a[1] is the second element, and so on.

For a multidimensional array, specify additional subscripts. For example, if a is a two-dimensional array, a[0][0] is the first element.

Examples

(1) This computed label creates an empty array then adds three elements.
var a = new Array();
a[0] = "one";
a[1] = true;
a[2] = 3;
var display = "";
for(var i=0; i<a.length; i++) {
	display = display + a[i] + " ";
}
display // one true 3
(2) This computed label creates an array with three undefined elements.
var a = new Array(3);
var display = "";
for(var i=0; i<a.length; i++) {
	if(i[0] == "") i[0] = "undefined";
	display = display + a[i] + " ";
}
display // undefined undefined undefined
(3) This computed label creates an array with three elements.
var a = new Array("one", true, 3);
var display = "";
for(var i=0; i<a.length; i++) {
	display = display + a[i] + " ";
}
display // one true 3
(4) This computed label creates a two-dimensional array with nine elements.
var b = new Array(1, 2, 3);
var a = new Array(b, b + b.length, b + 2 * b.length);
var display = "";
for(var i=0; i<a.length; i++) {
	for(var j=0; j<b.length; j++) {
		display = display + a[i][j] + " ";
	}
}
return display // 1 2 3 4 5 6 7 8 9
(5) This computed label creates an array with three elements using literal notation.
var a = ["one", true, 3];
var display = "";
for(var i=0; i<a.length; i++) {
	display = display + a[i] + " ";
}
display // one true 3
(6) This computed label creates an array with three undefined elements using literal notation.
var a = [3];
var display = "";
for(var i=0; i<a.length; i++) {
	if(i[0] == "") i[0] = "undefined";
	display = display + a[i] + " ";
}
display // undefined undefined undefined
(7) This computed label creates a two-dimensional array with nine elements using literal notation.
var b = [1, 2, 3];
var a = [b, b + b.length, b + 2 * b.length];
var display = "";
for(var i=0; i<a.length; i++) {
	for(var j=0; j<b.length; j++) {
		display = display + a[i][j] + " ";
	}
}
return display // 1 2 3 4 5 6 7 8 9