Back to Function Challenge
Code Studio · Level 6

Real JavaScript Arrays

Pack one lunchbox with several foods. An array keeps a whole ordered list inside one JavaScript variable.

15-20 min Ages 9-12 Real Code JavaScript

What is an array?

An array is like a lunchbox with numbered spaces. One variable holds the whole lunch, while each food has its own index.

Important: JavaScript starts counting at 0, so the first food is lunchBox[0].

Open bento lunchbox
[0]Sandwich
[1]Cookies
[2]Carrots
  1. 1

    Make the list

    let lunchBox = ["sandwich", "cookies", "carrots"];

  2. 2

    Grab a slot by number

    lunchBox[0] gets the sandwich. The number inside the brackets is called an index.

  3. 3

    Run the lunch code

    Each say() command reads one indexed food and packs it into the matching lunchbox space.

Code Editor

Pack a JavaScript lunch.

Example code
let lunchBox = ["sandwich", "cookies", "carrots"];

say(lunchBox[0]);
say(lunchBox[1]);
say(lunchBox[2]);
Lunchbox Array

Run the code to pack lunch.

Open empty bento lunchbox
lunchBox[0] Sandwich
lunchBox[1] Cookies
lunchBox[2] Carrots
Lunchbox is empty

Use the example, then run it to fill all three array slots.