Arrays Lesson

An array stores multiple values in one ordered list. Each value has an index, and the first index is zero. Arrays are useful when a program needs to manage related items such as scores, names, objects, images, or tasks.

Arrays can grow and shrink while the program runs. Methods like push and pop change the list, while loops let the program process each item in order.

Code Runner Challenge

Arrays Lesson

View IPYNB Source
%%js

// CODE_RUNNER: Arrays Lesson

const inventory = ["key", "map"];
inventory.push("flashlight");

for (let i = 0; i < inventory.length; i++) {
  console.log("Inventory item: " + inventory[i]);
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

The code below treats an inventory as an ordered list. Adding an item changes the array, and the loop displays every current item.