Break and Continue in javascript, are jump statement used to transfer execution control to another part of program.
Break
Break is used to come out of switch statement or breaks the iteration of a loop, when specific condition is met.
Switch Example
For switch and break details, learn switch statement
<script>
let day = 1;
switch(day)
{
case 1:
document.write("Monday");
break; // Jumps out of switch
case 2:
document.write("Tuesday");
break; // Jumps out of switch
default:
document.write("Holiday");
}
</script>
Loop Example
<script>
let i;
for(i = 0; i < 10; i++)
{
if(i == 5)
break; // Jumps out of loop
document.write(i);
}
</script>
In loop, when value of i equals to 5, if condition becomes true, break statement is there, so, as it encounters break statement, execution control jump out of loop and executes the rest of the statement.
Continue
It is used in loops to skip the current iteration (loop cycle) of loop when specific condition is met and continues the next loop iteration.
<script>
let i;
for(i = 0; i < 10; i++)
{
if(i == 7)
continue; // skips current iteration of loop and continues the loop
document.write(i);
}
</script>
In above example, loop starts executing, and when the value of i become 7, if condition becomes true and inside if continue is there, so, it skips the current cycle and continues the of loop.