JavaScript Arrays: Create, Read and Loop Through Lists
Learn how to create JavaScript arrays, access items by index, add values and loop through a list with for...of.
Create an array
An array stores an ordered list of values. Each item has a zero-based index, so the first item is at index 0.
const colors = ["blue", "green", "orange"];
console.log(colors[0]); // "blue"
console.log(colors.length); // 3
Add or update items
Use push() to add an item to the end. Use an index when you need to replace an existing item.
colors.push("purple");
colors[1] = "teal";
Loop through a list
A for...of loop reads each value in order. It works well when you want to perform the same action for every item.
for (const color of colors) {
console.log(color);
}
Use arrays for ordered collections. For named properties that describe one thing, use an object.