o Take, for example, days of the week. In each day we perform a number of tasks, so we could want to record each task as a separate item under a group called, say, Monday’s Tasks.
o It could be helpful to think of an array as a row of mail boxes in an office, just as you might think of a variable as a single, solitary mail box.
o The boxes in an array are numbered upwards, starting at box number 0 – note that counting begins at 0 here, just as we discussed in the previous chapter. The number assigned to each box is known as its index.
arrDays = new Array();
This statement creates a new, empty array called arrDays. We can call arrays just like we can variables, but with a few minor adjustments.
o If you already know how many elements a given array will have, you can declare this explicitly:
arrDays = new Array(7);
o More useful, however, is the ability to “fill” the boxes of an array when you create it. For example:
arrDays = new Array(“Monday”,”Tuesday”);
We now have an array with two elements. The first (element 0) has a value of “Monday”, while the second (element 1) has a value of “Tuesday”. We need not restrict ourselves to string values in arrays – Boolean, numerical and string values are allowed, as in arrays. It is even possible to assign other arrays to array elements – more on this later.
o The most often-used way of creating an array is to use “square bracket” notation. Square brackets play a large role in the use of arrays, so this is often the easiest method to remember:
arrDays = [“Monday”,”Tuesday”];
This is identical to the previous example.
To access an array’s elements, we first call the array’s name, and then specify the number of the element in square brackets, like so:
Note the lack of quotes around the 0 here. This line of code is equivalent to:
assuming the array is defined as in the previous examples.
o Not only can we access the value of an array element using this notation, but we can also assign the value as we
arrDays = “Tuesday”; arrDays = “Wednesday”;
o If you wish to add an element to an array without knowing the index of the last element, you can use the following code:
arrDays = “some other day”;
how_many_days = arrDays.length;
o As well as properties, arrays have very useful methods. If you wished to sort your array alphanumerically, you could use the array’s sort method thus:
Note that the sort method works on the actual array itself, overwriting the current values. So, if you had an array with each day of the week as an element, calling its sort method would mean that arrDays was then equal to “Friday”, not “Monday”.
o Open your previous project file, and save it under the name chapter_15.html.
o Store the names of the months of the year in an array.
Use an array method to sort the array elements alphanumericall
o Use a for loop to iterate through each array element in turn, and print the value of the element to the screen (hint, consider the use of array[i], where i is the for loop’s counter).
o To reinforce this generalism, if you have not used the array.length value in your loop, consider its use now. To prove that you have created a more generic loop, try the code with an array of days instead of an array of months, and see if you have to change any of the looping code.