start learning
Image 1

for of loop

The "for...of loop" in JavaScript is a looping construct introduced in ECMAScript 2015 (ES6). It allows you to iterate over the values of iterable objects, such as arrays, strings, maps, sets, and more. Unlike the "for...in loop," which iterates over object properties, the "for...of loop" focuses on iterating over the actual values of an iterable.

The basic syntax of a "for...of loop" in JavaScript is as follows:


for (variable of iterable) {
    // Code to execute for each value
}

Here's how the "for...of loop" works:

  1. The loop iterates over each value of the iterable object.
  2. In each iteration, the variable is assigned the value of the current element being processed.
  3. The code block inside the loop is executed for each value.
Example:

var numbers = [1, 2, 3, 4, 5];

for (var num of numbers) {
    console.log(num);
}

In this example, the loop iterates over the values of the numbers array and prints each value.

The "for...of statement" is particularly useful when you want to work with the individual values of an iterable without worrying about the iteration details. It's a more concise and intuitive way to loop through arrays and other iterable objects compared to traditional "for" loops.