Platform Support
IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|
4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Constructors
Constructor | Action | IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|---|---|
Array Constructor(Number arrayLength, Number elementN) : Array
Constructs a new instance of an array.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Properties
Property | Action | IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|---|---|
constructor : Object
Specifies the function that creates the Array prototype.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
index : Number
Zero-based index number for the corresponding string in a RegExp match.
|
Show Details | no | 1.0+ | 3.0+ | 7.0+ | no |
input : String
String in a RegExp match.
|
Show Details | no | 1.0+ | 3.0+ | 7.0+ | no |
length : Number
Integer specifying the number of elements in an array.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
prototype : Object
Represents the Array prototype object.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Functions
Method | Action | IE | Mozilla | Netscape | Opera | Safari |
---|---|---|---|---|---|---|
concat(Number valueN) : Array
Returns a new array comprised of this array joined with other array(s)
and/or value(s).
|
Show Details | 4.0+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
every(Function callback, Function thisObject) : Boolean
Returns true if every element in an array meets the specified criteria.
|
Show Details | no | 1.0+ | 3.0+ | no | no |
filter(Function callback, Function thisObject) : Array
Creates a new array with all elements that meet the specified criteria.
|
Show Details | no | 1.0+ | 3.0+ | no | no |
forEach(Function callBack, Object thisObject) : Object
Executes the specified function once for each element in an array.
|
Show Details | no | 1.0+ | 3.0+ | no | no |
indexOf([Object searchElement,] [Number fromIndex]) : Number
Returns the first index number at which the specified element can be found
in the array. Returns -1 if the element is not present.
|
Show Details | no | 1.0+ | 3.0+ | no | no |
join([String separator]) : String
Joins all elements of an array into a string.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
lastIndexOf([Object searchElement,] [Number fromIndex]) : Number
Searches an array backwards starting from fromIndex and returns the last
index number at which the specified element can be found in the array.
Returns -1 if the element is not present.
|
Show Details | no | 1.0+ | 3.0+ | no | no |
map(Function callBack, Object thisObject) : Array
Creates a new array with the results of calling a provided function on
every element in this array.
|
Show Details | no | 1.0+ | 3.0+ | no | no |
pop() : Object
Removes the last element from an array and returns that element. This method
changes the length of the array.
|
Show Details | 5.5+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
push(Object element1, ..., elementN) : Number
Adds one or more elements to the end of an array and returns the new length
of the array. This method changes the length of the array.
|
Show Details | 5.5+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
reverse() : Array
Transposes the elements of an array: the first array element becomes the
last and the last becomes the first.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
shift() : Object
Removes the first element from an array and returns that element. This
method changes the length of the array.
|
Show Details | 5.5+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
slice(Object begin, Object end) : Array
Extracts a section of an array and returns a new array.
|
Show Details | 4.0+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
some([Object searchElement,] [Number fromIndex]) : Boolean
Returns true if some element in the array passes the test implemented by
the provided function.
|
Show Details | no | 1.0+ | 4.0+ | no | no |
sort([Function compareFunction]) : Array
Sorts the elements of an array.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
splice(Object index, [Number howMany,] Object element1, ..., elementN) : Array
Returns the array with the specified elements inserted or removed.
|
Show Details | 5.5+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
toLocaleString() : String
Returns a localized string version of the array.
|
Show Details | 5.5+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
toSource() : String
Returns a string representing the source code of the array.
|
Show Details | no | 1.0+ | 3.0+ | no | no |
toString() : String
Returns a string representing the specified array and its elements.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
unshift(Object element1, ..., elementN) : Number
Adds one or more elements to the beginning of an array and returns the
new length of the array.
|
Show Details | 5.5+ | 1.0+ | 4.0+ | 7.0+ | 1.0+ |
valueOf() : Boolean|Number|String
Returns the primitive value of an array or the primitive value of its elements.
|
Show Details | 4.0+ | 1.0+ | 3.0+ | 7.0+ | 1.0+ |
Creating an Array
The following example creates an array, msgArray
, with a length of 0, then assigns values to msgArray[0]
and msgArray[99]
, changing the length of the array to 100.
var msgArray = new Array(); msgArray[0] = "Hello"; msgArray[99] = "world"; // The following statement is true, // because defined msgArray[99] element. if (msgArray.length == 100) myVar = "The length is 100.";
Creating a Two-dimensional Array
The following creates a two-dimensional array and assigns the results to
myVar
.
var myVar = "Multidimensional array test; "; a = new Array(4); for (var i = 0; i < 4; i++) { a[i] = new Array(4); for (var j = 0; j < 4; j++) { a[i][j] = "[" + i + "," + j + "]"; } } for (var i = 0; i < 4; i++) { str = "Row " + i + ":"; for (var j = 0; j < 4; j++) { str += a[i][j]; } myVar += str + "; "; }
The following string is assigned to myVar
(line breaks are used here for readability):
Multidimensional array test; Row 0: [0,0][0,1][0,2][0,3]; Row 1: [1,0][1,1][1,2][1,3]; Row 2: [2,0][2,1][2,2][2,3]; Row 3: [3,0][3,1][3,2][3,3];
Remarks
An array is an ordered set of values associated with a single variable name.
The following example creates an Array object with an array literal; the coffees array contains three elements and has a length of three:
coffees = ["French Roast", "Columbian", "Kona"];
You can construct a dense array of two or more elements starting with index 0 if you define initial values for all elements. A dense array is one in which each element has a value. The following code creates a dense array with three elements:
myArray = new Array("Hello", myVar, 3.14159);
Indexing an array
You index an array by its ordinal number. For example, assume you define the following array:
myArray = new Array("Wind", "Rain", "Fire");
You can then refer to the elements as thus:
myArray[0]
is the first elementmyArray[1]
is the second elementmyArray[2]
is the third element
Specifying a single parameter
When you specify a single numeric parameter with the Array
constructor, you specify the initial length of the array. The following
code creates an array of five elements:
billingMethod = new Array(5);
The behavior of the Array
constructor depends on whether the single parameter is a number.
- If the value specified is a number, the constructor converts the number to an unsigned, 32-bit integer and generates an array with the length property (size of the array) set to the integer. The array initially contains no elements, even though it might have a non-zero length.
- If the value specified is not a number, an array of length 1 is created, with the first element having the specified value.
The following code creates an array of length 25, then assigns values to the first three elements:
musicTypes = new Array(25); musicTypes[0] = "R&B"; musicTypes[1] = "Blues"; musicTypes[2] = "Jazz";
Increasing the array length indirectly
An array's length increases if you assign a value to an element higher than the current length of the array. The following code creates an array of length 0, then assigns a value to element 99. This changes the length of the array to 100.
colors = new Array(); colors[99] = "midnightblue";
Creating an array using the result of a match
The result of a match between a regular expression and a string can create an array. This array has properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.replace. To help explain these properties and elements, look at the following example and then refer to the table below:
// Match one d followed by one or more b's followed by one d // Remember matched b's and the following d // Ignore case myRe = /d(b+)(d)/i; myArray = myRe.exec("cdbBdbsbz");
The properties and elements returned from this match are as follows:
Property/Element | Description | Example |
input |
A read-only property that reflects the original string against which the regular expression was matched. | cdbBdbsbz |
index |
A read-only property that is the zero-based index of the match in the string. | 1 |
[0] |
A read-only element that specifies the last matched characters. | dbBd |
[1], ...[n] |
Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited. | [1]: bB [2]: d |
Availability
JavaScript 1.1|JScript 2.0|ECMAScript v1