loop throught items in javascript

In JavaScript, you can loop through items in an array using different types of loops. Here are a few examples:

  1. Using a for loop:
index.tsx
const items = ["item1", "item2", "item3"];

for(let i = 0; i < items.length; i++) {
    console.log(items[i]);
}
113 chars
6 lines
  1. Using a for...of loop (loops over the values):
index.tsx
const items = ["item1", "item2", "item3"];

for(const item of items) {
    console.log(item);
}
96 chars
6 lines
  1. Using the forEach method (available on arrays):
index.tsx
const items = ["item1", "item2", "item3"];

items.forEach((item) => {
    console.log(item);
});
97 chars
6 lines

These are common ways to loop through items in JavaScript.

related categories

gistlibby LogSnag