JS: Module 4

← Dashboard
Module 4: The Repeat Order

Loops & Arrays

Store lists in arrays. Loop with for. Use push, pop, filter, map.

// Array - inventory of flavors
const flavors = ["Espresso", "Latte", "Mocha"];

// For loop
for (let i = 0; i < flavors.length; i++) {
  console.log(flavors[i]);
}

// Array methods
flavors.push("Americano");      // add
const hot = flavors.filter(f => f !== "Iced");
Pro Tip

forEach, map, filter return new arrays. Use them instead of manual loops when you can.

Index

Arrays start at 0. flavors[0] is the first item.