How to Write a For Loop in JavaScript
Use a for loop when code needs to repeat a known number of times or move through an array by index. This is the cleanest pattern for counters, index-based updates, and controlled repetition.
What you’ll build or solve
You’ll learn how to write a for loop in JavaScript with the start value, condition, and increment step. You’ll also know how to loop through arrays safely.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when the number of repetitions is known or tied to array length.
Common real-world scenarios include:
- Array iteration
- Countdown timers
- Pagination rendering
- Grid generation
- Repeated DOM updates
This is a bad idea when you only need to loop array values directly. In that case, for...of or array methods may read better.
Prerequisites
You only need:
- A JavaScript file or browser console
- Basic arrays and variables knowledge
Step-by-step instructions
Step 1: Add the start, condition, and increment
A for loop has three parts inside the parentheses.
JavaScript
for (let i = 0; i < 5; i++) {
console.log(i);
}
The loop starts at 0, runs while the condition stays true, and increases by 1 after each cycle.
This same structure works well for arrays.
JavaScript
const names = ["Alex", "Mia", "Jordan"];
for (let i = 0; i < names.length; i++) {
console.log(names[i]);
}
What to look for:
- Start value runs once
- Condition is checked before every cycle
- Increment updates the counter
i < array.lengthprevents overflow- Great for index-based logic
Examples you can copy
Count to 10
JavaScript
for (let i = 1; i <= 10; i++) {
console.log(i);
}
Loop array
JavaScript
for (let i = 0; i < tasks.length; i++) {
console.log(tasks[i]);
}
Reverse countdown
JavaScript
for (let i = 5; i > 0; i--) {
console.log(i);
}
Common mistakes and how to fix them
Mistake 1: Wrong loop condition
What the reader might do:
JavaScript
for (let i = 0; i <= names.length; i++)
Why it breaks: the final cycle points to an index that does not exist.
Corrected approach:
JavaScript
for (let i = 0; i < names.length; i++)
Mistake 2: Forgetting the increment
What the reader might do:
JavaScript
for (let i = 0; i < 5;) {
console.log(i);
}
Why it breaks: i never changes, so the loop never ends.
Corrected approach:
JavaScript
for (let i = 0; i < 5; i++)
Mistake 3: Starting at the wrong index
What the reader might do:
JavaScript
for (let i = 1; i < names.length; i++)
Why it breaks: the first array item is skipped.
Corrected approach:
JavaScript
for (let i = 0; i < names.length; i++)
Troubleshooting
If the loop never stops, check the increment step.
If the last item is undefined, switch <= to <.
If the first item is skipped, start at 0.
If the loop only needs values, consider for...of.
Quick recap
- Use
forfor controlled repetition - Add start, condition, and increment
- Use
< array.lengthfor arrays - Start at
0for indexes - Great for counters and index logic
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot