JavaScript Simplified · One concept, one day, one project · Day 10 of 32
Arrays
Holding many values under one name, in order. You build a shopping list and learn to read and change any item in it.
By the end of today you can
- Create an array and understand its order
- Read a value by its index, remembering indexes start at 0
- Change a value and read how many items a list has
- Build a shopping list
A variable holds one value. But most real information comes in lists: items in a cart, students in a class, messages in a chat. An array is a single name that holds many values, kept in order.
1. What an Array Is
Definition
An array is an ordered list of values, written in square brackets and separated by commas. The whole list lives under one variable name.
2. Index: reading one value
Definition
Each item has a position called its index. Indexes start at 0, not 1. So the first item is index 0, the second is index 1, and so on. You read an item with its index in square brackets.
fruits[0] is "apple", the first item. The index is one less than the position, because counting starts at 0. This trips up every beginner once, then never again.
The first item is index 0, so in a list of three items the last index is 2, not 3. Asking for fruits[3] in a three-item list gives undefined, because there is nothing there.
3. Changing values and counting them
You can replace an item by assigning to its index. And .length tells you how many items are in the array.
fruits[1] = "orange" swaps "banana" for "orange". fruits.length is 3: the count of items. Note length is a count, so it is one more than the last index.Today’s Mini Project
🛒 Shopping List
Create a shopping list as an array, print the whole list, print the first and last items, and show how many things are on it.
shopping[shopping.length - 1] is the clean way to get the last item: the last index is always one less than the length, whatever the size.
Replace "salt" with "pepper", add a new item by setting shopping[4] = "yam", then print the list and its new length. Expected: a five-item list ending in yam.
Make an array of three countries you would like to visit, then print the second one.
Expected: the country at index 1. Remember, index 1 is the second item.
A list is everywhere in software: a feed, a cart, a playlist, a leaderboard. You can now hold one under a single name and reach any item by its position. Tomorrow you learn the tools that make lists grow and shrink.
Checkpoint
What index is the first item of an array?
Index 0. Indexes start at 0, so the first item is 0, the second is 1, and the last is length minus 1.
How do you find out how many items an array has?
With .length, for example fruits.length. It is a count, so it is always one more than the last index.
How do you get the last item of any array?
With array[array.length - 1]. The last index is always the length minus one, whatever the size of the array.
Tick Before You Move On
- ☐ I can create an array and print it
- ☐ I can read an item by its index, counting from 0
- ☐ I can change an item and read the length
- ☐ I built the Shopping List
Quick Recap
An array holds many ordered values under one name. Each item has an index, and indexes start at 0. You read or change an item with its index in square brackets. .length is the count of items, always one more than the last index.
Next: Day 11. Setting items by index is fine, but real lists grow and shrink. Tomorrow you meet array methods that add and remove items for you.