JavaScript Loops
A loop repeats work.
Instead of copying the same line ten times, you write one loop.
Think of a train going around a track until you say stop.
Why loops help
Counting loops
List loops
Careful with forever loops
Why loops exist
Repeating by hand is boring and easy to mess up.
Loops keep the steps in one place.
Counting loops
for and while are great when you know how to count.
You start, you test, you update.
List loops
Some loops walk through arrays or objects.
for...of, for...in, and forEach will show up soon.
break and continue
break leaves the loop early.
continue skips the rest of this round and goes to the next.
Avoid forever loops
If the stop condition never becomes false, the loop never ends.
Always make sure something moves toward the end.
Tip: Before writing a loop, say what should change each time and when it should stop.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.