🚂

What is an Array?

🎉
💨
Welcome aboard the Array Express! I'm Choo-Choo, your guide!

What is an Array?

An array is like a train with cars! Each train car holds one item, and they all travel together in a line.

In JavaScript, we use arrays to store lists of things - like your favorite fruits, your friends' names, or even numbers!

💡
💨

How Arrays Look

Arrays use square brackets [ ] to hold items, and each item is separated by a comma.

1// An array of fruits
2const fruits = ["🍎", "🍌", "🍊"];
3
4// An array of numbers
5const numbers = [1, 2, 3, 4, 5];
6
7// An empty array (no cars yet!)
8const empty = [];
🤔
💨

Array Index

Every car in our train has a number called an index. But here's a fun twist - we start counting from 0, not 1!

So the first item is at index 0, the second at index 1, and so on.

1const fruits = ["🍎", "🍌", "🍊"];
2// [0] [1] [2]
3
4fruits[0] // "🍎" - first item
5fruits[1] // "🍌" - second item
6fruits[2] // "🍊" - third item
🎮

Try It Yourself!

Here's your array train! Try adding more fruit cars by clicking the buttons below.

[
🍎
[0]
🍌
[1]
🍊
[2]
]
length: 3