Known Iterations
Use a for loop when you know how many times you need to run a block.
It combines initialization, condition, and increment in one place.
JavaScript Tutorial
The for loop is the most common loop when you know exactly how many times to iterate.
It keeps initialization, condition, and step in one concise line.
Index-based loops are helpful for arrays, pagination, and fixed counts.
A well-structured for loop is easy to read and efficient.
for (init; condition; step) { ... }for (let i = 0; i < 3; i++) {
console.log(i);
}A standard for loop counts from 0 to 2.
const items = ["a", "b", "c"];
for (let i = 0; i < items.length; i++) {
console.log(items[i]);
}Use indices when you need the position.
Use a for loop when you know how many times you need to run a block.
It combines initialization, condition, and increment in one place.
for loops are great for index-based iteration over arrays.
Be mindful of the start and end conditions to avoid off-by-one errors.
Nested loops are useful for grids and combinations, but can be expensive.
Keep the inner work light and avoid unnecessary nesting.
Without
let i = 0;
console.log(i);
i++;
console.log(i);
i++;With
for (let i = 0; i < 2; i++) {
console.log(i);
}Double-check loop boundaries (use < vs <= intentionally).
Cache length if needed for performance in hot loops.
Ensure the counter changes so the loop can finish.
Avoid changing the array you are iterating unless necessary.
Initialization, condition, and increment (step).
Be explicit about whether the end index is inclusive or exclusive.
When you only need the values, not the index.
Practice: Print the first 10 even numbers using a for loop.
// TODO: print 0 to 18 step 2
One Possible Solution
for (let i = 0; i < 20; i += 2) {
console.log(i);
}Use for when you need the index, for...of when you only need values.
Yes, you can count down with i--.
Often similar; choose readability first unless performance is critical.
Try changing the loop bounds and step to see how iteration changes.