Documentation

Student-friendly HTML, CSS, and JavaScript reference with examples

Sections

For-Of Loop Basics

A for-of loop goes through each element in an array, one by one.

for (const item of array) {
  // code runs for each item
}

How It Works

The variable (item) gets each value from the array:

const numbers = [10, 20, 30];
for (const num of numbers) {
  console.log(num);
}
// Prints: 10, then 20, then 30

With Strings

const names = ["Alice", "Bob", "Carol"];
for (const name of names) {
  console.log(name);
}

Key Points

  • Use const for the loop variable (it changes each iteration)
  • The loop runs once for each element
  • You get the value directly, not the index