Back to Function Challenge
Code Studio · Level 6

Real JavaScript Arrays

One backpack, many treasures. Arrays hold whole lists — your inventory, the high scores, every enemy on screen — inside a single variable.

15-20 min Ages 9-12 Real Code JavaScript

What is an array?

An array is a backpack with numbered pockets. One variable holds the whole pack, and every item sits in its own slot. The slots count from 0 — coders always start counting at zero!

🗺️ 🍎 🔦 [0] [1] [2] backpack
  1. 1

    Pack the bag: let backpack = ["map", "snack", "flashlight"];

    The square brackets [ ] make a list, and the commas separate the items. One variable — backpack — now carries all three things at once.

  2. 2

    Grab a slot by number

    backpack[0] is the first item (map!), backpack[1] the second, backpack[2] the third. The number in brackets is called the index — and index 0 always means "first."

  3. 3

    Why coders use them

    A game's inventory, every enemy on screen, the top-10 scores — all arrays. And arrays love loops: one loop can visit every slot no matter how full the backpack gets. Try adding a fourth item and saying backpack[3]!

Code Editor

Store a list of items.

Example code
let backpack = ["map", "snack", "flashlight"];

say(backpack[0]);
say(backpack[1]);
say(backpack[2]);
Output

Program result

Robot ready to speak your strings

Type real JavaScript, then run it.