languages/why-does-my-javascript-loop-run-one-time-too-many.md

Why does my JavaScript loop run one time too many?

Programming languages · 8 min read ·

A loop tells JavaScript to repeat some code until a condition says stop. A for loop packs three pieces onto one line: a setup that runs once, a condition checked before every lap, and an update that runs after every lap.

Step through for and while loops to watch the counter change on each lap, then play with start, end, step and the < vs <= switch. Along the way you'll see where the classic off-by-one bug comes from, and the one-character fix.