Video Lesson

Lesson Notes

Looping Over Array Elements with for...of in JavaScript

You can use the for...of loop for a concise way to iterate over every element in an array without managing an index.

Syntax

1const names = ["John", "Mary", "Susie", "Jay", "Edward", "Mike"]; 2 3for (let value of names) { 4 console.log(`Here is the name: ${value}`); 5}
  • for (let value of names)
    • value takes on each element in the names array in turn
    • No need to initialize, check length, or increment an index

How It Works

  1. The loop starts by assigning the first element ("John") to value.
  2. Executes the loop body (console.log).
  3. Moves to the next element ("Mary"), repeats until the end of the array.
  4. Stops automatically when all elements have been processed.

Why Use for...of?

  • Less code: Eliminates index boilerplate.
  • Readability: Clearly expresses “do this for each element.”
  • Safety: Cannot accidentally go out of bounds or skip elements.

AI Assistant

Sign In Required

Sign in and subscribe to use the AI assistant for instant help with your lessons.

Sign In

Array Iteration: Using `for...of` in JavaScript

In this lesson, you'll discover the simplicity of the `for...of` loop in JavaScript, learning how to efficiently iterate through array elements without the hassle of managing indexes. By the end, you'll appreciate how this concise syntax enhances code readability and safety, making your programming experience smoother and more intuitive.

2m 5s

Course Content

0 of 32 lessons completed
6. Promises, Async and Await
0 of 0 completed
7. The DOM
0 of 0 completed