Categories
JavaScript Answers

How to loop through a set of elements in JavaScript?

Spread the love

To loop through a set of 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 list array with a for-of loop.

Then we get the item being looped through and log it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *