Foundations
Arrays
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
scoresstarts as[82, 91, 76].bonusstarts at5.firstScore = scores[0]reads82.adjustedScore = scores[1] + bonusadds91 + 5.- The program prints
first=82andadjusted=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.