Open In App

What is For…of Loop in JavaScript ?

In JavaScript, the for...of loop is a language construct used to iterate over elements of an iterable object, such as arrays, strings, maps, sets, and other data structures that implement the iterable protocol. It provides a more concise and readable syntax compared to traditional for loops or forEach() methods.

Syntax:

for (variable of iterable) {
// code block to be executed
}

Example: Here, we have an array arr containing numeric values. The for...of loop iterates over each element of the array, assigning the value of each element to the variable value, and then executes the code block inside the loop, which simply logs each value to the console.




const arr = [1, 2, 3];
for (const value of arr) {
  console.log(value); // Outputs: 1, 2, 3
}

Output
1
2
3
Article Tags :