[prev]Arrays[next]

An array is a useful way of storing an ordered list of data.

Example: Array of Favorite Bands

 
1
2
3
4
5
6
7
8
9
10
11
var bands = [ "Fall Out Boy", "Usher", 
              "Linkin Park", "Ne-yo",
              "Alicia Keys" ]

printp("Favorite Bands:")
print(bands)

printp("My favorite band is: ", 
       bands[0])
printp("My least favorite band is: ",
       bands[4])

Click to run the code.

In this example, we create an array (bands) in lines 1-3; print the array in line 6; and print two different items that we extract from the array in lines 8-11.

Note the [ ] syntax (called "square brackets" or sometimes just "brackets") for creating an array. A list of values or variables inside brackets, separated by commas, becomes an array that can be stored in a variable.

Getting items from an array also uses brackets. Given that bands is an array, bands[0] is the first item, bands[1] is the second item, and so on. Because there are five items in the array, the last one is bands[4]. The number in brackets is called an "index".

Why does the first item have index 0?

It may seem strange at first to start counting with 0, because this kind of numbering is rare outside of computer-land. Not all programming languages use 0-based numbering, but the majority do. It comes from the early days of computers, where if three things were stored in an array at memory location x, their locations in memory were x+0, x+1, and x+2. And depending on the situation, it can make a lot of sense. For example, in some parts of the world, midnight is considered "0" o'clock.

Exercise: Arrays of arrays? Can one array be an item in another array? Try it! (An array made up of only other arrays is often called a "two-dimensional array".)

Length of Arrays

All arrays have a "property" called length representing the number of items. You can access this using the "." property accessor we talked about before, as in bands.length. Getting the length of an array can be really useful for generating the condition of a loop.

Example: Looping Over an Array

 
1
2
3
4
5
6
7
8
9
10
var bands = [ "Fall Out Boy", "Usher", 
              "Linkin Park", "Ne-yo",
              "Alicia Keys" ]

printp("There are ", bands.length,
       " bands.")

for (var i = 0; i < bands.length; i++) {
  printp("Band ", i, "", bands[i])
}

Click to run the code.

Note that the above example uses i++ instead of i = i + 1 for the increment of the loop. Both statements are ways of increasing the value of i by 1.

Exercise: Reverse order looping. By changing only the for-loop parameters, print the bands array in reverse order, starting with "Alicia Keys", and ending with "Fall Out Boy".

Challenge: Reverse array. Write code that reverses the order of elements in the bands array.

Note: A hint.

You'll need to "swap" the element at index i of the array with the element at the corresponding index bands.length - 1 - i. You'll need an extra variable to store one of the values while you swap the other. Can you figure out how to do that in a loop?


Arrays are useful when you have a list of data, but what about when you have multiple pieces of data that describe a single thing? That's where objects come in, described in the next chapter.

Note: Off-by-one errors and the Fencepost Problem.

A very common class of programmer error with loops is to miss the first or last element of an array while looping over it. For example, if we replace i < bands.length above with i <= bands.length, this would create an off-by-one error. (What happens when you do this and try to run it?)

A similar problem is called the "fencepost problem" — when counting the length of a fence, do you count the number of "posts" or the number of "sections"? See this wikipedia article for more.

Previous Lesson: Ifs Next Lesson: Objects