for...of
When you want to work with the value directly in each iteration and do not require the index at all, you can use a for...of loop.
for...of works like the basic for loop shown above, but instead of having to deal with the index as a variable in the loop, you are provided with the value directly.
const numbers = [6.0221515, 10, 23];
// Because re-assigning number inside the loop will be very
// confusing, disallowing that via const is preferable.
for (const number of numbers) {
console.log(number);
}
// => 6.0221515
// => 10
// => 23
Just like in regular for loops, you can use continue to stop the current iteration and break to stop the execution of the loop entirely.
Backlinks