Introduction to Arrays

An array is an ordered collection of items. The items can be of any JavaScript type, including strings, numbers, and booleans. The following example shows the beginning of the alphabet, an ordered collection of the strings "a", "b", "c", "d", and "e":

var letters = ["a", "b", "c", "d", "e"];

We could also create an array of the first five numbers: javascript var numbers = [1, 2, 3, 4, 5];

And you can mix between the two: javascript var lettersandnumbers = ["string", 10, true, "what?"];

Each element of an array has a position, and the counting starts at zero. It's worth repeating: the counting starts at zero, not one. In the example array letters, "a" is at position 0, "b" is at position 1, "c" is at position 2, and so forth:

a b c d e
0 1 2 3 4

The numbers 0, 1, and 2 are referred to as array indexes. The following code can be used to get the value at index 2 of the letters array.

var letters = ["a", "b", "c", "d", "e"];
console.log(letters[2]); // "c"

The following code can be used to get the first value of the people array.

var people = ["bob", "frank", "liz"];
console.log(people[0]); // "bob"

Remember that the first element of any array is always at index position 0.

Arrays have a length property that returns the number of elements in an array. The following sports array has three elements so the length property returns the integer 3.

var sports = ["soccer", "football", "baseball"];
console.log(sports.length); // 3
Question Click to View Answer

What does the following code print to the console?

var cars = ["toyota", "honda", "volvo"];
console.log(cars[1]);

honda

Index position 1 corresponds with the second element of the array, which is the string "honda".

Print the first element of the following array to the console.

var simpsons = ["homer", "lisa", "bart"];

console.log(simpsons[0]);

Index position 0 corresponds with the first element of the array.

What does the following code print to the console?

var fun = ["party"];
console.log(fun.length);

1

There is only one element in the party array, so the length property returns 1.

What does the following code print to the console?

var arrestedDev = ["tobias", "michael", "gob"];
console.log(arrestedDev[arrestedDev.length - 1]);

gob

The arrestedDev array has three elements, so the length property returns 3. The length of an array minus one equals the index position of the last element in the array :)

<-- Previous (If Else Statements) Next (While Loops) -->