Arrays
Lists of values.
A shopping list, a playlist, the scores in a game. Programs are full of lists. In JavaScript, a list is called an array, and it keeps its values in order.

- Shopping lists, playlists, scores
- Programs are full of lists
- Arrays
Making and reading arrays
Square brackets make an array, with values separated by commas. Each value has a position, called an index, starting at zero. So fruits at zero is the first fruit. Length tells you how many items there are, and length minus one is the last index. Run it.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits);
console.log(fruits[0]);
console.log(fruits.length);
console.log(fruits[fruits.length - 1]);
console.log(fruits.at(-1));[ 'apple', 'banana', 'cherry' ] apple 3 cherry cherry
Changing items
You can replace an item by assigning to its index, like fruits at one equals blueberry. That works even though fruits is a const, because const stops you replacing the whole array, not changing what's inside it. Arrays can hold any type, even a mix, but usually they hold one kind of thing.
| key | value |
|---|---|
| [a, b, c] | make an array |
| arr[0] | first item |
| arr.length | how many |
| arr.at(-1) | last item |
Summary
Arrays hold values in order, starting at index zero. Length counts them, and at minus one gets the last. Next, adding and removing items.
Key takeaways- [ ] makes an array
- Index from 0
- length and at(-1)