Real JavaScript Arrays
One backpack, many treasures. Arrays hold whole lists — your inventory, the high scores, every enemy on screen — inside a single variable.
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!
-
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
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
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]!
Store a list of items.
let backpack = ["map", "snack", "flashlight"];
say(backpack[0]);
say(backpack[1]);
say(backpack[2]);
Program result
- Run your code to see output.
Type real JavaScript, then run it.
You used JavaScript to keep several values in one ordered list.
Open Array Builder