Break and Continue
Break and Continue is a core JavaScript concept covering break and Continue: The break statement "jumps out" of a loop. This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.
The Break Statement
The break statement "jumps out" of a loop.
for (let i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
The Continue Statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
for (let i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
Labels
To jump out of a nested loop, you can use labels.
list: {
text += cars[0] + "<br>";
text += cars[1] + "<br>";
break list; // Jumps out of the list block
text += cars[2] + "<br>";
}
Labels
Labels are rarely used in modern JavaScript development, but it's good to know they exist.