December 3, 2026 - 08:49
JavaScript Lesson 09 — for, for...of and forEach: Work Through a List Image
JavaScript Tutorials

JavaScript Lesson 09 — for, for...of and forEach: Work Through a List

0 Comments

Printing each array element on a separate handwritten line works only while we know the list's size. A loop repeats an operation for the items actually present.

Use for...of when you need the values

JAVASCRIPT
const topics = ['HTML', 'CSS', 'JavaScript'];
for (const topic of topics) {
  console.log(topic);
}
for (let index = 0; index < topics.length; index += 1) {
  console.log(index, topics[index]);
}

The first loop takes one array value per iteration and binds it to topic. It prints HTML, CSS and JavaScript in order. It does not rebuild the array, and we do not need to calculate an index.

The indexed loop also exposes each position. It starts at zero, continues while the index is below the length, and increments after each iteration. index += 1 adds one and reassigns the result. You should see 0 HTML, 1 CSS and 2 JavaScript.

Changing the comparison to <= creates one extra iteration. That final access reads a nonexistent element and produces undefined. Think through the last valid zero-based index instead of automatically adding one.

Why does forEach receive a function?

JAVASCRIPT
const topics = ['HTML', 'CSS', 'JavaScript'];
function showTopic(topic) {
  console.log(topic);
}
topics.forEach(showTopic);

We define showTopic first. ForEach then calls it once per array element and supplies that element as an argument. A function supplied for another operation to call is a callback.

We write topics.forEach(showTopic), without parentheses after showTopic. Writing showTopic() would call it immediately and pass its return value instead. The difference between a function reference and a call matters here.

Trace an indexed loop one turn at a time

Index starts at zero. The condition zero less than three succeeds, so the body runs. The increment then changes the index to one before the condition is checked again. At three, the condition fails and there is no fourth body execution.

The increment occurs after the body. Removing it can leave the condition permanently true. Trace an intentional infinite-loop mistake on paper rather than freezing a browser to observe it.

Break stops; continue skips the remaining turn

JAVASCRIPT
const topics = ['HTML', 'CSS', 'Linux'];
for (const topic of topics) {
  if (topic === 'CSS') {
    continue;
  }
  console.log(topic);
}
for (const topic of topics) {
  console.log(topic);
  if (topic === 'CSS') {
    break;
  }
}

The first prints HTML and Linux. The second prints HTML and CSS, then stops. Move the break check before logging and CSS is no longer printed. Statement order matters.

Return belongs to the callback invocation

JAVASCRIPT
const topics = ['HTML', 'CSS', 'Linux'];
topics.forEach(function (topic) {
  if (topic === 'CSS') {
    return;
  }
  console.log(topic);
});

This prints HTML and Linux. Returning skips the rest of that callback invocation, not subsequent invocations. Break inside this callback is not a valid way to exit an outer loop. Choose a loop supporting the early exit you need.

Producing data is another job

ForEach is not a transformed-array result. Later we will use map and filter for particular data operations. A for...of loop pushing into a separate result array can first expose the underlying steps explicitly.

Ask whether you need values, indexes, early stopping, a new result or an effect such as logging. These needs explain the available choices better than declaring one loop universally best.

Search for Linux and stop after one matching message, then repeat with an empty array. No iterations on empty input are normal, not proof that the loop mechanism failed.

Choose according to the job

ForEach can read clearly for a small action on every item. A for...of loop is useful when you need to stop at the first match using break. Returning from a forEach callback ends that callback call, not the entire traversal. For a new transformed array we will later use map; forEach does not return that array.

An ordinary for loop with a missing increment or incorrect condition can run forever. Begin with small lists and clear stopping conditions.

Add Linux and check that all three approaches process it. Then modify the for...of example to stop after CSS using a condition and break. Do not assume a return inside forEach would perform the same early exit.

References: Reference 1 · Reference 2

Related Articles

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment