Beginner · 8 min
Work with arrays
Store an ordered collection and access or add items.
Read an item
An array stores an ordered list inside square brackets. Indexing starts at 0. The length property reports the number of items.
const cities = ["Rome", "Oslo", "Lima"];
console.log(cities[0]);
console.log(cities.length);Output
Rome 3
Add an item
push() adds an item at the end. A const binding cannot be reassigned, but the contents of an array stored in it can change. join() combines the items into a string.
const tasks = ["Read", "Practice"];
tasks.push("Review");
console.log(tasks.join(", "));Output
Read, Practice, Review
Put it into practice
- Predict the output of each example before running it.
- Complete the coding task, then try the practice questions.
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Extend a shopping list
Add "Milk" to items with push(), then display the list joined with ", ".
Call items.push("Milk") before displaying the list.