for (initialiser; condition; final-expression) { // code to run }
for (let i = 0; i < 100; i++) {
}
Javascript offer several ways to loop over a given collection, like an array.
The for...of loop is a basic tool for looping over a collection.
const names = ["Paul", "Henry", "Tanya"];
for (const name of names) {
console.log(name);
}
Do something for each item in a collection and create a new collection
with map().
const upperNames = names.map(toUpper);
Test each item in a collection, and create a new one with only the matching items
with filter().
const filteredNames = names.filter(name => name.startsWith("P"));
while (i < names.length) {
// code to run
i++;
}
let i = 0;
do {
// code to run
i++;
} while (i < names.length);