Lets you work with arrays.
var arr1 = new Array(arrayLength); var arr2 = new Array(element0, element1, ..., elementN);
Array literals use the form:
var lit = [element0, element1, ..., elementN];
arrayLengthelementNAn array is an ordered set of values associated with a single variable name. Note that you shouldn't use it as an associative array, use Object instead.
The following example creates an Array object with an array literal; the coffees array contains three elements and has a length of three:
var coffees = ["Kenyan", "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:
var myArray = new Array("Hello", myVar, 3.14159);
You index an array by its ordinal number. For example, assume you define the following array:
var 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 elementWhen 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:
var billingMethod = new Array(5);
The behavior of the Array constructor depends on whether the single parameter is a number.
The following code creates an array of length 25, then assigns values to the first three elements:
var musicTypes = new Array(25); musicTypes[0] = "R&B"; musicTypes[1] = "Blues"; musicTypes[2] = "Jazz";
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.
var colors = new Array(); colors[99] = "midnightblue";
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
var myRe = /d(b+)(d)/i;
var 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 |
For properties inherited by Array instances, see Properties of Array instances.
For methods inherited by Array instances, see Methods of Array instances.
Array instancesArray instances inherit from Array.prototype. As with all constructors, you can change the constructor's prototype object to make changes to all Array instances.
Array prototypes because it is not inherited by default. It is, in fact, only present in arrays created by regular expression matches. The property represents the zero-based index of the match in the string.Object:__defineGetter__, __defineSetter__, eval, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, toSource, toLocaleString, toString, unwatch, valueOf, watch
These methods modify the array:
These methods do not modify the array and return some representation of the array.
Several methods take as arguments functions to be called back while processing the array. When these methods are called, the length of the array is sampled, and any element added beyond this length from within the callback is not visited. Other changes to the array (setting the value of or deleting an element) may affect the results of the operation if the method visits the changed element afterwards. While the specific behavior of these methods in such cases is well-defined, you should not rely upon it so as not to confuse others who might read your code. If you must mutate the array, copy into a new array instead.
Many methods on the JavaScript Array object are designed to be generally applied to all objects which "look like" Arrays. That is, they can be used on any object which has a length property, and which can usefully be accessed using numeric property names (as with array[5] indexing). Some methods, such as join, only read the length and numeric properties of the object they are called on. Others, like reverse, require that the object's numeric properties and length be mutable; these methods can therefore not be called on objects like String, which does not permit its length property or synthesized numeric properties to be set.
Introduced in JavaScript 1.6
Object:__defineGetter__, __defineSetter__, eval, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, toLocaleString, unwatch, valueOf, watch
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";
if (msgArray.length == 100)
print("The length is 100.");
The following creates chess board as a two dimensional array of strings. The first move is made by copying the 'P' in 6,4 to 4,4. The position 4,4 is left blank.
var board =
[ ['R','N','B','Q','K','B','N','R'],
['P','P','P','P','P','P','P','P'],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
['p','p','p','p','p','p','p','p'],
['r','n','b','q','k','b','n','r']];
print(board.join('\n') + '\n\n');
// Move King's Pawn forward 2
board[4][4] = board[6][4];
board[6][4] = ' ';
print(board.join('\n'));
Here is the output:
R,N,B,Q,K,B,N,R P,P,P,P,P,P,P,P , , , , , , , , , , , , , , , , , , , , , , , , , , , , p,p,p,p,p,p,p,p r,n,b,q,k,b,n,r R,N,B,Q,K,B,N,R P,P,P,P,P,P,P,P , , , , , , , , , , , , , , , , , ,p, , , , , , , , , , p,p,p,p, ,p,p,p r,n,b,q,k,b,n,r
Page last modified 06:20, 12 Mar 2010 by Sevenspade