loop
/ loop /
A loop is code that repeats an action instead of making you write it out over and over. If you need to greet 100 people, you don't type 100 greetings — you write the greeting once and wrap it in a loop that runs it 100 times. It's the programmer's answer to 'do this again, and again, and again.'
Loops come in two everyday flavors. One walks through a collection, doing the same thing once for each item in a list — handy when you have an array and want to touch every element. The other keeps going while some condition stays true, and stops the instant it turns false; the condition is the loop's brake pedal.
The thing to watch for is forgetting to stop. If the condition never becomes false — or you never reach the end of the list — the loop runs forever, freezing your program. An 'infinite loop' is the classic beginner stumble, and the fix is always the same: make sure something inside the loop pushes it toward the exit.
for (let i = 1; i <= 3; i++) {
console.log("hello " + i);
}
// hello 1
// hello 2
// hello 3Run the same line three times — i counts up from 1 to 3, then the loop stops.
A loop that never ends is called an infinite loop — usually a bug, occasionally on purpose (like a server waiting for requests).