Array Object (JavaScript)

Provides support for creation of arrays of any data type.

arrayObj = new Array()
arrayObj = new Array([size])
arrayObj = new Array([element0[, element1[, ...[, elementN]]]])

arrayObj

Required. The variable name to which the Array object is assigned.

size

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

element0,...,elementN

Optional. The elements to place in the array. This creates an array with n + 1 elements, and a length of n + 1. Using this syntax, you must supply more than one element.

After an array is created, you can access the individual elements of the array by using [ ] notation, for example:

var my_array = new Array();
for (i = 0; i < 10; i++) {
    my_array[i] = i;
}
x = my_array[4];

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 must be an unsigned 32-bit integer (< approximately four billion). That value then becomes the size of the array. If the value is a number, but is less than zero or is not an integer, a run-time error occurs.

If a single value is passed to the Array constructor, and it is not a number, the length property is set to 1, and the value of the only element becomes the single, passed-in argument.

Notice that JavaScript arrays are sparse arrays, that is, although you can allocate an array with many elements, only the elements that actually contain data exist. This reduces the amount of memory used by the array.

The Array object was introduced in Internet Explorer before Internet Explorer 6. Some members in the following lists were introduced in later versions.

The following table lists the properties of the Array object.

Property

Description

constructor Property

Specifies the function that creates an object.

length Property (Array)

Returns an integer value that is one higher than the highest element defined in an array.

prototype Property

Returns a reference to the prototype for a class of objects.

The following table describes the function of the Array object.

Function

Description

Array.isArray Function

Returns a Boolean value that indicates whether an object is an array.

The following table lists the methods of the Array object.

Method

Description

concat Method (Array)

Returns a new array consisting of a combination of two arrays.

every Method

Checks whether a defined callback function returns true for all elements in an array.

filter Method

Calls a defined callback function on each element of an array, and returns an array of values for which the callback function returns true.

forEach Method

Calls a defined callback function for each element in an array.

hasOwnProperty Method

Returns a Boolean value that indicates whether an object has a property with the specified name.

indexOf Method (Array)

Returns the index of the first occurrence of a value in an array.

isPrototypeOf Method

Returns a Boolean value that indicates whether an object exists in another object's prototype chain.

join Method

Returns a String object consisting of all the elements of an array concatenated together.

lastIndexOf Method (Array)

Returns the index of the last occurrence of a specified value in an array.

map Method

Calls a defined callback function on each element of an array, and returns an array that contains the results.

pop Method

Removes the last element from an array and returns it.

propertyIsEnumerable Method

Returns a Boolean value that indicates whether a specified property is part of an object and whether it is enumerable.

push Method

Appends new elements to an array, and returns the new length of the array.

reduce Method

Accumulates a single result by calling a defined callback function for all elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

reduceRight Method

Accumulates a single result by calling a defined callback function for all elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

reverse Method

Returns an Array object with the elements reversed.

shift Method

Removes the first element from an array and returns it.

slice Method (Array)

Returns a section of an array.

some Method

Checks whether a defined callback function returns true for any element of an array.

sort Method

Returns an Array object with the elements sorted.

splice Method

Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.

toLocaleString Method

Returns an object converted to a string using the current locale.

toString Method

Returns a string representation of an object.

unshift Method

Inserts new elements at the start of an array.

valueOf Method

Returns the primitive value of the specified object.

Community Additions

Show:
© 2014 Microsoft