How to loop through a set of array elements in JavaScript?

Sometimes, we want to loop through a set of array elements in JavaScript.

In this article, we’ll look at how to loop through a set of array elements in JavaScript.

How to loop through a set of array elements in JavaScript?

To loop through a set of array elements in JavaScript, we use a for-of loop.

For instance, we write

const list = [
  { a: 1, b: 2 },
  { a: 3, b: 5 },
  { a: 8, b: 2 },
  { a: 4, b: 1 },
  { a: 0, b: 8 },
];

for (const item of list) {
  console.log(item);
}

to loop through the items in list with a for-of loop.

We get the item being looped through from item.

Conclusion

To loop through a set of array elements in JavaScript, we use a for-of loop.