Coding Basics: Arrays

Example 1:

a = [2, 3, 4];

b = a[0] + a[1];    //b holds the value 5.  Notice that the index for the values in the array start at 0.

Example 2:

c = [2, 7, 8, 12];

d = c.length;   //d holds the value 4 since there are 4 values in the array.

Example 3:

e = [ ];

e.push(4);

e.push(7);

e.push(8);

//e holds the values [4, 7, 8].  Notice push sends the value to the end of the array.

Example 4:

f = [3, 4, 5];

g = f[0] + f[f.length-1];      //g holds the value 8 since this is f[0] + f[2].

Example 5:

sum = 0;

h = [3, 11, 13];

for (i = 0; i < h.length; i++)

{

   sum = sum + h[0];

}

//here sum holds the value 27 (the sum of 3, 11 and 13).