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]]]])
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.
The Array object was introduced in
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. Also supported in Windows Store apps. 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 methods of the Array object.
|
Method |
Description |
|---|---|
|
Returns a new array consisting of a combination of two arrays. |
|
|
Checks whether a defined callback function returns true for all elements in an array. |
|
|
Calls a defined callback function on each element of an array, and returns an array of values for which the callback function returns true. |
|
|
Calls a defined callback function for each element in an array. |
|
|
Returns a Boolean value that indicates whether an object has a property with the specified name. |
|
|
Returns the index of the first occurrence of a value in an array. |
|
|
Returns a Boolean value that indicates whether an object exists in another object's prototype chain. |
|
|
Returns a String object consisting of all the elements of an array concatenated together. |
|
|
Returns the index of the last occurrence of a specified value in an array. |
|
|
Calls a defined callback function on each element of an array, and returns an array that contains the results. |
|
|
Removes the last element from an array and returns it. |
|
|
Returns a Boolean value that indicates whether a specified property is part of an object and whether it is enumerable. |
|
|
Appends new elements to an array, and returns the new length of the array. |
|
|
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. |
|
|
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. |
|
|
Returns an Array object with the elements reversed. |
|
|
Removes the first element from an array and returns it. |
|
|
Returns a section of an array. |
|
|
Checks whether a defined callback function returns true for any element of an array. |
|
|
Returns an Array object with the elements sorted. |
|
|
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. |
|
|
Returns a string using the current locale. |
|
|
Returns a string representation of an array. |
|
|
Inserts new elements at the start of an array. |
|
|
Gets a reference to the array. |