What is the purpose of break and continue?
JavaScript
The short answer
break exits a loop entirely — no more iterations run. continue skips the current iteration and jumps to the next one. Both give you control over loop execution flow.
break
JAVASCRIPT
1for (let i = 0; i < 10; i++) {2 if (i === 5) break; // exits the loop3 console.log(i);4}5// Output: 0, 1, 2, 3, 4
Common use: stop searching once you find what you need:
JAVASCRIPT
1const users = [2 { name: 'Alice' },3 { name: 'Bob' },4 { name: 'Charlie' },5];6let found;78for (const user of users) {9 if (user.name === 'Bob') {10 found = user;11 break; // no need to check the rest12 }13}
continue
JAVASCRIPT
1for (let i = 0; i < 10; i++) {2 if (i % 2 === 0) continue; // skip even numbers3 console.log(i);4}5// Output: 1, 3, 5, 7, 9
Common use: skip items that do not meet a condition without nesting inside if:
JAVASCRIPT
1for (const item of items) {2 if (!item.active) continue;3 processItem(item);4}
In switch statements
break is also used in switch to prevent fall-through:
JAVASCRIPT
1switch (color) {2 case 'red':3 console.log('Stop');4 break; // without this, it falls through to the next case5 case 'green':6 console.log('Go');7 break;8}
Interview Tip
Show one example of each with a for loop. Mention that break is also used in switch statements. This is a basic question — keep it short.
Why interviewers ask this
This tests fundamental control flow knowledge. It is a simple question usually asked in early-career interviews.