for each loop in javascript

In JavaScript, the for each loop is not used. Instead, we can use the for...of loop to iterate over the elements of an array.

index.tsx
const array = [1, 2, 3, 4];

for (const element of array) {
  console.log(element);
}

// Output: 
// 1
// 2
// 3
// 4
119 chars
12 lines

The for...of loop will iterate over each element of the array and assign it to the element constant. We can then use the element variable to perform whatever operation we want on each element of the array.

Note, however, that the for...of loop only works for iterable objects like arrays, maps, and sets. It will not work for objects or strings.

gistlibby LogSnag