Arrays keep related values in order and let code read values by index.

Arrays

arrays.js
const scores = [82, 91, 76];
const bonus = 5;
const firstScore = scores[0];
const adjustedScore = scores[1] + bonus;

console.log("first=" + firstScore);
console.log("adjusted=" + adjustedScore);

Follow the Array

  1. scores starts as [82, 91, 76].
  2. bonus starts at 5.
  3. firstScore = scores[0] reads 82.
  4. adjustedScore = scores[1] + bonus adds 91 + 5.
  5. The program prints first=82 and adjusted=96. | bonus | first score | adjusted score | | --- | --- | --- | | 0 | 82 | 91 | | 5 | 82 | 96 | | 10 | 82 | 101 |
array index Array indexes start at zero, so `scores[0]` reads the first value.

Exercise: arrays.js

Reproduce first=82 and adjusted=96, then try bonus 0 and 10 and predict each adjusted score before running it.