JavaScript tutorial:
Array object

 

Methods: concat Method, join Method, reverse Method, slice Method, sort Method, toString Method, valueOf Method

Properties: constructor Property, length Property, prototype Property

The Array object is provide support for creation of arrays of any data type.

Syntax

new Array()
new Array(
size)
new Array(element0, element1, ..., elementn)

The Array object creation syntax has these parts:

Part

Description

size

The size of the array. As arrays are zero-based, created elements will have indexes from zero to size -1.

element0,...,elementn

The elements to place in the array. This creates an array with n + 1 elements, and a length of n.

Example

After an array is created, the individual elements of the array can be accessed using [ ] notation, for example:

document.write("The all the elements in my_array are: ");
my_array = new Array();
for (i = 0; i < 10; i++)
{
my_array[i] = i;
document.write(my_array[i]);
}
document.write("x array is: ");
x = my_array[4];
document.write(x);

  To run the code above, paste it into JavaScript Editor, and click the Execute button.

Since arrays in JavaScript are zero-based, the last statement in the preceding example accesses the fifth element of the array. That element contains the value 4.

If only one argument is passed to the Array constructor, and the argument is a number, it is coerced into an unsigned integer, and the value is used as the size of the array. Otherwise, the parameter passed in is used as the only element of the array.

See also: new Operator