There are a few different ways to create an array. The first three involve the Array( ) constructor, and the last is the array literal.

The Array Constructor
The array 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 Array( ) constructor, you create an empty array with no elements. Since JavaScript arrays are dynamic, this is not a problem.

Dynamic just means you don't have to tell JavaScript in advance how large the array will be.

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 Array( ) constructor, you create an array with the specified number of elements. None of the elements have any value assigned to them.

The code above creates an array with six elements, all of which have a value of undefined.

What this method does is set the length of the array to the value specified.

You can check the array length by testing the value of arrayName.length, where arrayName is replaced by the name of the array to be tested.

Multiple argument method

var aName = new Array(20, 1, 7, 'first', 'last');

If you declare a variable to be an array and pass more than one arguement with the Array( ) constructor, you create an array with the number of elements being equal to the number of data values specified.

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 "last".

Array Literals

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, 'first', 'last'];

Whenever you assign a list of values enclosed in square brackets to a variable in a variable declaration statement, JavaScript assumes that you are declaring an array.

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.

Multi-Dimensional Arrays

JavaScript doesn't actually have support for multi-dimensional arrays, but you can nest arrays. For most programming purposes, nested arrays are equivalent to multi-dimensional arrays. Therefore we are not going to worry about the differences between them here.

To create a nested array, you just define an array element as being an array.

var outerA = new Array();
outerA[0] = new Array();
outerA[1] = new Array();
outerA[2] = 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[0] = [1,2,3];
aName[1] = ['a','b','c'];

Now let's look at an example of a simple array, and view source to see the code.

And now a nested array.

And now, a page with forms and an array.

(from here)

Table of Contents