JavaScript Loop Control — Interview Questions & Answers
Ques: What is loop control in JavaScript?
Ans: Loop control statements are used to change the normal flow of a loop — to exit early, skip iterations, or control nested loops.
Main loop control statements:
breakcontinuelabel(used with break/continue)
Ques: What is the break statement in JavaScript?
Ans: The break statement is used to exit a loop immediately, even if the loop condition is still true.
Example:
for (let i = 1; i <= 5; i++) {
if (i === 3) break;
console.log(i);
}
Output:
1
2
Ques: What is the continue statement?
Ans: The continue statement skips the current iteration and moves to the next loop cycle.
Example:
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
Output:
1
2
4
5
Ques: What is the difference between break and continue?
| Feature | break | continue |
|---|---|---|
| Purpose | Exits the loop completely | Skips the current iteration |
| Control Flow | Jumps out of the loop | Moves to next iteration |
| Use Case | When loop should stop | When one condition should be ignored |
Example:
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // skip 3
if (i === 4) break; // stop at 4
console.log(i);
}
Output:
1
2
Ques: What are labelled statements in JavaScript?
Ans: A label gives a name to a loop (or block) and allows break or continue to refer to specific outer loops in nested structures.
Syntax:
labelName: statement
Example:
outerLoop:
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (i === 2 && j === 2) break outerLoop;
console.log(`i=${i}, j=${j}`);
}
}
Output:
i=1, j=1
i=1, j=2
i=1, j=3
i=2, j=1
Ques: Can continue be used with labels?
Ans: Yes, When using nested loops, continue with a label allows jumping to the next iteration of the labeled loop.
Example:
outer:
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) continue outer;
console.log(`i=${i}, j=${j}`);
}
}
Output:
i=1, j=1
i=2, j=1
i=3, j=1
Ques: When should you use labelled statements?
Ans: Use labels only when absolutely necessary — like when controlling nested loops where simple break or continue isn’t enough.
Avoid them in simple loops — they reduce readability and can confuse flow.
Ques: Are break and continue allowed outside loops or switch statements?
Ans: No, Both break and continue can only be used inside:
- Loops (
for,while,do...while) switchstatements (forbreakonly)
Ques: Can you use return instead of break in functions?
Ans: Yes, inside a function, return immediately exits the function — similar to how break exits a loop.
However:
breakonly affects loops or switchreturnaffects function execution
Ques: What are best practices for loop control statements?
- Use
breakandcontinuefor clarity, not complexity. - Avoid deeply nested loops — refactor with functions.
- Use labels sparingly and name them clearly.
- Ensure loop conditions and exit logic are well-defined.
