JavaScript fundamentals / 05
Loops, break, and continue
Replace repeated statements with for, while, and do...while loops, then control iteration with break and continue.
On this page
Why use loops?
A loop repeats an action. It is useful whenever we want the same statements to run more than once. Printing "ong" a single time is simple, but copying the same statement ten times is inefficient:
console.log("ong");
console.log("ong");
console.log("ong");
console.log("ong");
console.log("ong");
console.log("ong");
console.log("ong");
console.log("ong");
console.log("ong");
console.log("ong");
A loop expresses that repetition directly. This is the original article's example:
for(int i = 0; i < 10; i++){
console.log("ong");
}
Editorial correction: JavaScript does not declare variables with
int. In the example above, changeint i = 0tolet i = 0. The original code is retained to make the correction explicit.
The structure of for
A for loop has three parts separated by semicolons:
- An initialization, such as
let i = 0. - A condition that determines whether the next iteration runs.
- An update expression, such as
i++, that runs after each iteration.
In this example, i starts at zero and increases by one each time. The loop runs while i < 10 and stops when i reaches ten, printing "ong" ten times. It does not wait until i exceeds ten.
while and do...while
A while loop puts its condition in parentheses and stops when that condition is false. The original example initializes a to zero and repeatedly writes "ong", incrementing a while the stated condition allows it.
Open the original while example on CodePen.
A do...while looks similar, but the condition appears after the body. This changes the order: while checks first and then runs the body; do...while runs the body first and checks afterward. As a result, the body of a do...while runs at least once.
Open the original do...while example on CodePen.
break and continue
You may remember break from the switch lesson. In a loop, it exits the loop entirely.
continue does something different: it skips the remainder of the current iteration. For example, in a loop that counts upward toward ten, encountering break when i is four leaves the loop. Encountering continue skips to the loop's update and then the next iteration, where i becomes five.
The original closing sentence previewed variable scope; the next published article in this archive introduces functions.
Moved here from Tistory
I migrated this post from my Korean Tistory blog, I am Jason Lee, to this website and translated it into English. My writing and projects now live together in one place.
The original publication date, code examples, and screenshots are preserved. Editorial notes clarify known issues in the original material.
Original post on Tistory English edition · Aug 30, 2026