Arrays in JavaScript are dynamic

Like all scripting languages​​, JavaScript has dynamic arrays: their size is not predetermined, nor the type of data.

They are declared either by assigning a literal, or with a constructor

And the reserved word new.

var x = ["a", "b", "c"];    // literal

var x = new Array();        // constructor
var y = new Array("a", "b", "c"); // constructor with items

Note that if the Array() constructor contains only one numerical element, this element defines the size of the array and is not an element of the array.

var x = new Array(5);    // empty array with 5 slots.
var x = new Array(1, 2); // array holding two items, the 1 and 2 numbers.

This may seem not very coherent but it did not matter, because if we want to create an array of some elements, we use a literal instead. Example:

var x = [5];             // array holding only one item, the 5 number.

One can verify it with the following example.

An array is declared with the Array(5) constructor. The array with an empty content is outputed, then its size.
Then the Array(1,2) constructor with two numbers is outputed.

var x = new Array(5);
document.writeln(x);
document.writeln(x.length);

x = new Array(1, 2);
document.writeln(x);

The literal value of an array may have variables

The list of items is placed between square brackets and contains any values, numbers, strings, objects, separated by a comma.
The commas are used to specify the number of elements, they can contain blank spaces:

x = [ "a", , , , "e"]

The letter "e" is in position 4 in the array. Ending commas are ignored.

A variable can be given like element of a literal array, then its contents will be assigned dynamically to the array.

For a two-dimensions literal array, the syntax is:

x = [[ "a", "b", "c"] , [ 1,2,3]] 

Indexing an array may gives it its size

The first element has position 0. One assigns or reads the contents of an array with the index of position between square brackets.

var x = new Array();
alert(x[0]);     // reading
x[0] = "car"; // assigning

If another position of the array had been assigned, for example at position 5, the array would have been automatically redimensioned with a size of 6 elements at least, if its former size were null or less than 6 items.

Array added as item to another makes it multidimensional

A multidimensional is build by creating an array of elements which are other arrays.

var a = new Array(
new Array(1, 2, 3),
new Array(4, 5, 6));
document.write(a);

Result:

var a = [ [ 1, 2, 3], [ 4, 5, 6] ];
document.write(a);

Result:

a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;
document.write(a);

Result:

The item in a multidimensional array may be accessed with chains of indices, for example:

document.write(a[1][2]);

That should output 6.

Assigning an array to a variable, value or reference?

When we pass an array as an argument to a function, the array is passed by reference. In other words, if you change the array in the function, the original array passed to the function is changed.

But when you assign an array to a variable, it is assigned by reference or by value? We'll do the test with the following code:

var x = ["one", "two", "three"];
var y = x;
x[3] = "four";
document.write(y);

We create the array x, then assigns it to y. We adds a new element "four" to x. We realize that this new element is also in y!

If you want to copy an arrya, uses instead the slice method that returns a copy of the array (not a reference to it):

x = ["one", "two", "three"];
var z = x.slice();
x[3] = "four";
document.write(z);

This time we see that adding a new element x, z which was assigned before x remains unchanged.

The same principle would be used if you wanted to pass an array by value to a function:

myfunction(x.slice()); 

Additional note:

On modern browsers can display a table formatted with the JSON.stringify function:

JSON.stringify(x, null, 4);  

See also

© 2007-2014 Xul.fr