JavaScript loops and iteration
Last updated: January 12, 2023
Introduction
OK. So, there are more ways to iterate over things…
Recap
…
Array.prototype.forEach() Function signature
JavaScript
someArray.forEach( function(currentItem, currentIndex, fullArray) {
// do things
});
Array.prototype.forEach()
JavaScript
const apples = ["McIntosh", "Granny Smith", "Fuji", "Red Delicious", "Honey Crisp"];
apples.forEach( function(currentApple, appleIndex, fullApplesArray) {
console.log('→ ', currentApple, appleIndex, fullApplesArray);
});
→
Live examples
See the Pen HTML cheatsheet 1 / display types by perpetual.education (@perpetual-education) on CodePen.
Do what while what?
MDN: do…while statement (loop)
Let’s do these before the for loop – because they are simple… but have more to consider in a way… and the for loop kinda encapsulates it all.
for statement
…
Break and Continue
…