Note that all external links will open up in a separate window.
This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.
array: a composite data type that holds a collection of data elements than can be referenced by their index number
An array is a composite data type that stores a collection of values. These values can then be referenced by an index number.
The index number is the location of the data element in the array. Think of it as a big box with numbered sub-compartments that you can put things in. You can then say that the thing you are looking for is in the compartment the bears the desired number, such as number six.
Each stored value so indexed is referred to as an array element.
Don't let this flexibility distract you from the fact that arrays exist to organize groups of data into a single data element. This normally makes collections of data easier to work with, as well as clarifying their relationship.
There are a few different ways to create an array. The first three involve the
The Array Constructor
The arrays constructor is used on the right hand side of a combined variable declaration and assignment statement. It takes the following form:
var aName = new Array( );
If you declare a variable to be an array and do not pass any arguments with the
// single argument method var aName = new Array(6);
If you declare a variable to be an array and pass a single integer as an argument with the
The code above creates an array with six elements, all of which have a value of
What this method does is set the
You can check the array length by testing the value of
// multiple argument method var aName = new Array(20, 1, 7, 'top', 'love');
If you declare a variable to be an array and pass more than one arguement with the
Each element will have the value specified in the list for that position. The above array will have five elements with the first element having a value of 20 and the fifth element having a value of "love".
What happens if you want to create an array that just has one integer element ? The answer is that you can't, at least not with the
Since you can have empty elements, so you can just use
Instead of using the Array constructor, an array literal allows you to assign an array of values directly to a variable. This is done by including those values in square brackets in the declaration and assignment statement.
var aName = [20, 1, 7, 'top', 'love'];
// array with undefined values var aName = [1,,3,,5]; // array with length of 1 var aName = ['bob']; // array assigned to an existing variable var aName = 752; aName = [1,2,3,4,5];
You should try to use the Array constructor instead of array literals when possible. If nothing else, it makes your code much easier to read. Easy to read code tends to hide fewer mistakes.
To create a nested array, you just define an array element as being an array.
var outerA = new Array(); outerA = new Array(); outerA = new Array(); outerA = new Array();
When using array literals, the same effect can be achieved by nesting square brackets.
var aName = [[1,2,3],['a','b','c']]; // is the same as var aName = new Array(); aName = [1,2,3]; aName = ['a','b','c'];