While looping statements on their own are powerful tools, the real advantage of loops is realised in combination with arrays. Arrays will be discussed in detail later, but for now, a brief introduction will be beneficial.
Technically speaking, an array is a series of like-typed variables that share a common name and are differentiated by an associative indexing element. Translated into English, an array is a series of variables that are grouped together by a common name.
When I introduced variables, I made the analogy between a variable and a folder. To abuse the analogy further, an array, then, is like a concertina folder. Imagine a regular variable as a single folder with the name 'employeeSalary' on it. A piece of paper could then be placed inside the folder with an employee's salary on it. In code, the example would look like this:
employeeSalary = 32000;
Now, imagine an organ folder named employeeSalary with 10 tabs, numbered from front to back, zero through to nine. You could then say, 'place in tab five of the employeeSalary folder the value '46,000'. The above code would be written as:
employeeSalary[5] = 46000;
You could then store salary information for all 10 employees in the one folder. It is clear to see that having one variable name that holds several values is not only logical, but saves time and space as well - imagine having to create 10 different variables for salaries, or even 10,000!
Most languages have a separate data type for arrays. JavaScript, on the other hand, utilises the concept of associative arrays to achieve the same effect. To use arrays in JavaScript, you use the name of the array (as you would any variable) immediately followed by an index element. For the moment, we will only discuss numeric indices. For example, if we had an array called 'customer' which had five elements, we could assign a name to each element like this:
customer[0] = "Tony Harper"; customer[1] = "Kathryn Houlahan"; customer[2] = "Lisa Caroll"; customer[3] = "Les Claypool"; customer[4] = "Matt McCaffree";
Notice how the indices begin at zero and count up to four, instead of beginning at one and counting to five. Beginning array indices with zero is an historical anomaly that is related to how the computer accesses its internal memory.
To create a new array object, you have to use the Array() constructor. For example, to make an array called myArray, you define a variable called 'myArray', and then use the 'new' keyword to create a new array object. Then, assign the new object to the variable 'myArray' using the assignment operator. All of this can be accomplished in the same line, however, as the following code illustrates.
var myArray = new Array(3);
The above code creates a new array object (myArray) with three elements. Each of the three elements can then be assigned a value by typing:
Syntax: myArray[index] = value |
or
myArray[0] = "This is the first element.";
This article has presented many new and potentially confusing ideas. But the text presented here is really just the introduction to the lesson; the bulk of the learning should come from working through the examples. If you're confused, try glancing through the article again and then move to the examples.