ChApter 1 ■ JAvASCrIpt You Need to KNow
The output of Listing 1-28 follows:
Andrew
Monica
Catie
Jenna
Christopher
As you can see, this approach hinges on the use of the Array.length property, looping through from 0 to the very
last index in the array.
Modifying array values is the same as modifying the values of any other variable, with the exception that you
need to know its location within the array. Listing 1-29 shows how we can update the entire array by adding the family
surname to each item.
Listing 1-29. Modifying Array Values
<!DOCTYPE html>
The most important part of this listing is myArray[i] = myArray[i] + " Grant";. All we do here is append the
family surname to the existing value at position i at each pass through the loop. Notice that I also log the entire array
to the console both before and after I modify the array’s contents. Passing the array to console.log() is a handy way
to dump the contents of the entire array for inspection. The output is as follows:
Before: ["Andrew", "Monica", "Catie", "Jenna", "Christopher"]
After: ["Andrew Grant", "Monica Grant", "Catie Grant", "Jenna Grant", "Christopher Grant"]