Indices

So you have your array of data elements, but what if you want to access a specific element? That is where indices come in. An index refers to a spot in the array. indices logically progress one by one, but it should be noted that the first index in an array is 0, as it is in most languages. Brackets [] are used to signify you are referring to an index of an array.

  1. // This is an array of strings
  2. var fruits = ["apple", "banana", "pineapple", "strawberry"];
  3. // We set the variable banana to the value of the second element of
  4. // the fruits array. Remember that indices start at 0, so 1 is the
  5. // second element. Result: banana = "banana"
  6. var banana = fruits[1];

{% exercise %}
Define the variables using the indices of the array
{% initial %}
var cars = [“Mazda”, “Honda”, “Chevy”, “Ford”]
var honda =
var ford =
var chevy =
var mazda =
{% solution %}
var cars = [“Mazda”, “Honda”, “Chevy”, “Ford”]
var honda = cars[1];
var ford = cars[3];
var chevy = cars[2];
var mazda = cars[0];
{% validation %}
assert(honda === “Honda”);
assert(ford === “Ford”);
assert(chevy === “Chevy”);
assert(mazda === “Mazda”);
{% endexercise %}