LESSON 19

Arrays

Learning objective: Store ordered lists of values in arrays.

Understand

Arrays hold multiple values in one variable.

An array is an ordered list. Each item has an index starting at 0. You can read items by index, get the count with .length, and add items with .push().

Analogy: An array is like a numbered row of lockers, each holding one value.

See It in Action

Creating and reading an array:

const fruits = ["apple", "pear"];
console.log(fruits[0]);     // apple
fruits.push("mango");
console.log(fruits.length); // 3
How it works: fruits[0] reads the first item, push adds a new one, and .length reports how many there are.

Try It Yourself

  1. Create an array of three items.
  2. Read the second item by index.
  3. Push a new item and log the length.

Quick Quiz

What index is the first array element?

Challenge

Build an array of your goals and log how many there are.

Success condition: Your array stores items and reports the correct length.