Arrays

Because objects are collections of properties with each property having its own name and value, arrays are actually JavaScript objects. Each property in an array is an element, and each element can be assigned a value. One way to think of an array is as a collection of numbered variables.

An array in JavaScript has the following general formats for assigning values to elements:

						sampleArr[0]=1; 
sampleArr[1]="ice cream"; 
sampleArr[2]=55 * (7 + alpha); 
sampleArr[3]= shootTheMoon(73); 
sampleArr[4]= otherArr[7]; 

or

						sampleArr=new Array(1,"ice cream", (55 * (7+ alpha)), shootTheMoon(73), 
otherArr[17]); 

or, in JavaScript 1.2 or later:

						sampleArr=[1,"ice cream", (55 * (7+ alpha)), shootTheMoon(73), otherArr[17]]; 

All three arrays are identical, ...

Get JavaScript Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.