How to Loop Over an Array in Javascript?

javascript

There are several ways to loop over an array in Javascript.

Use for-of:

for (const item of theArray) {
  // do something
}

Use forEach:

array.forEach(item => {
  // do something
});

Use the old-fashioned for-loop:

for(let idx=0; idx<array.length; i++) {
  const item = array[idx];
  // do something
  // works well with async-await
}

Latest Questions

javascript How to Check if a Javascript Array Includes a Given Value? javascript How to Completely Uninstall Nodejs, and Reinstall From Scratch on Mac OS X? javascript How to Map Values of a Javascript Object to a New Object Using a Function?