Back
LearnJavaScript From ZeroArrays — storing many things in order

Arrays — storing many things in order

1 min read

One box is not enough

A variable holds one value. But what if you have 50 students, or 10 products? You do not make 50 variables — you use an array: a single list that holds many values, in order.

let fruits = ["apple", "banana", "mango"];

How do I read one item?

Each item has a position number called an index, and counting starts at 0 — not 1.

console.log(fruits[0]);      // apple
console.log(fruits[2]);      // mango
console.log(fruits.length);  // 3  → how many items

That "starts at 0" thing feels strange at first — everyone stumbles on it. Just remember: the first item is [0].

How do I add or remove items?

fruits.push("orange");   // add to the end
fruits.pop();            // remove the last one

How do I go through every item?

This is where loops and arrays become best friends:

for (let fruit of fruits) {
  console.log("I like " + fruit);
}

Whether the list has 3 items or 3000, this exact same code handles all of them.

An array is a numbered shelf: each spot has an index starting at 0, and you can add, remove, or walk through every item.

Tip