Export (0) Print
Expand All
13 out of 16 rated this helpful - Rate this topic

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. Note that arrays in JavaScript are zero-based.

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

// Output: 4

You can pass an unsigned 32-bit integer to the Array constructor to specify the size of the array. If the value is negative or not an integer, a run-time error occurs. If you run the following code, you should see this error in the Console.

var arr = new Array(10);
document.write(arr.length

// Output: 10

// Don't do this
var arr = new Array(-1);
arr = new Array(1.50); 

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.

var arr = new Array("one");
document.write(arr.length);
document.write("<br/>");
document.write(arr[0]);

// Output:
1
one

JavaScript arrays are sparse arrays, which means that not all the elements in an array may contain data. In JavaScript, only the elements that actually contain data exist in the array. This reduces the amount of memory used by the array.

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Some members in the following lists were introduced in later versions. For more information, see JavaScript Version Information or the documentation for the individual members.

The following table lists the properties of the Array object.

Property

Description

constructor Property

Specifies the function that creates an array.

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 an array.

The following table describes the functions 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 a string using the current locale.

toString Method

Returns a string representation of an array.

unshift Method

Inserts new elements at the start of an array.

valueOf Method

Gets a reference to the array.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.