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.
Rena
Your AI tutor. She walks you through each step.
Asking for help is part of coding. Rena reads your code and gives you a hint. She is free with an account: 20 hints a day.