Arrays
7 min · JavaScript
Hold many values under one name, and reach for the one you want.
Enemy 1 of 5
An array holds several values in order under one name, written inside square brackets. This code makes a list of three fruits and prints it. Press Run to see it.
Why this works
Every variable so far has held one value at a time. An array changes that: it holds several values in order, all under one name, inside square brackets [ ] separated by commas, a single labeled list of slots instead of one box.
Arrays are everywhere in real JavaScript code because so much real-world data is naturally "a bunch of things": a list of scores, a set of usernames, the items in a shopping cart. Instead of creating fruit1, fruit2, fruit3 as separate variables, you group them into one array and can loop over, search, or grow the whole collection at once.