Entry
How do I create an Array?
Jun 25th, 2002 16:43
Aaron Peterson, Martin Honnen, www.openproliferation.org/incor/ www.openproliferation.org/tutortools/
First note that Array objects were introduced in JavaScript1.1 so if
you (which is rather seldom) happen to use NN2 or IE3 the following
doesn't hold for you.
Otherwise the simpliest way for short arrays is usually to call the
Array constructor and pass the array elements as arguments as in
var a = new Array (0, 1, 2, 3, 4);
You can however first create an empty array
var a = new Array();
and then add elements
a[0] = 0; a[1] = 1; a[2] = 2; a[3] = 3; a[4] = 4;
JavaScript1.2 (around since version 4 browsers) also introduced Array
literals (similar to string literals e.g. 'string' or number literals
e.g. 2) so you also write
var a = [0, 1, 2, 3, 4]
to create an array meaning an array literal is just a list of array
values contained in square brackets.
Lastly you might find
var a = new Array (5);
which in that case creates a new Array of length 5 so if you pass only
one numeric paramenter to new Array() that is taken as the Array length
and not an Array element. But as Arrays can grow and shrink dynamically
defining a length when creating an array is rather unimportant.
AP added links to his sites that use arrays extensively.
incor complex site works well,
tutortools is very fussy... the array code appears to work, but I have
a lot of other bugs in it...