Hii, guys, welcome back. In this tutorial, I will discuss break and continue in JavaScript. In the previous tutorial, we learned loops in JavaScript. So, without any delay, let’s begin.
Break and continue in JavaScript
Like other programming languages, break and continue keywords are jump statements in JavaScript. Jump statements are also known as unconditional jumps. Now, let’s discuss the break and continue statements in detail.
Break in JavaScript
The break in JavaScript is used to terminate or end the execution of the loop depending upon some condition and the flow of control passes to the statement immediately after the loop, if any. It is used with loops inside the if or else block. The break statement is defined using the break keyword.
Syntax of Break in JavaScript
break;
Example of Break in JavaScript
for (let i=0;i<10;i++) { if(i==5) break; console.log (i); } console.log ('Loop Terminated!'); //Output //0 //1 //2 //3 //4 //Loop Terminated!
Here, in the above example, we see that the value of i is printed up to i=4. When I become 5, the break statement is executed and the loop execution stops. The program control reaches the statement immediately after the loop and hence, the message Loop Terminated! is printed.
We can also use a break statement in a switch statement to stop the execution of the next cases. To understand it more clearly, let’s see one example,
const ch=1; switch(ch) { case 1: console.log ('The number is One.'); break; case 2: console.log ('The number is Two.'); break; default: console.log ('Wrong Choice!'); } //Output //The number is One.
In the above example, the statement corresponding to case 1 is executed and hence, The number is One is printed.
If the break statement is omitted in the switch statement, then the execution continues and all the cases will be executed.
const ch=1; switch(ch) { case 1: console.log ('The number is One.'); case 2: console.log ('The number is Two.'); default: console.log ('Wrong Choice!'); } //Output //The number is One. //The number is Two. //Wrong Choice!
There is no break keyword used in a switch statement in the above example. So, the statement corresponding to case 1 is printed but due to the absence of the break keyword, the statements corresponding to the next case are also executed and hence, we are getting output statements of case 2 and default along with the statement of case 1.
If you don’t know what switch statements in JavaScript are, you can refer to our previous tutorials. There I have explained the switch statement in JavaScript in detail.
Let’s now discuss what a continue statement in JavaScript is.
Continue in JavaScript
The continue in JavaScript is used to skip the execution of statements of the loop for the current iteration and continue with the next iteration of the loop. Unlike the break statement, which terminates or ends the loop execution, the continue statement does not terminate the execution of the loop, it just skips the execution of the loop based on some conditions. The continue statement is defined using the continue keyword.
Syntax of Continue in JavaScript
continue;
Example of Continue in JavaScript
for (let i=0;i<10;i++) { if(i==5) continue; console.log (i); } console.log ('Loop Terminated!'); //Output //0 //1 //2 //3 //4 //6 //7 //8 //9 //Loop Terminated!
Here, in the above Continue in JavaScript example, we see that the value of i is printed up to i=4. When I become 5, the continue statement is executed and the loop execution for the current iteration is skipped, that’s why 5 is not printed. The execution of the loop continues after i=5, and we are getting values of i.
Finally, when the loop condition fails i.e., i=11, the loop ends, and the program control reaches the statement immediately after the loop, and hence, the message Loop Terminated! is printed. Unlike break statements, we cannot use continue statements with switch statements in JavaScript.
So, when you want to stop the execution of a loop based on some condition, then you can use a break statement but when you don’t want to end the loop, you just simply want to omit the execution of the loop for some condition, you can use a continue statement.
I hope you have understood the break and continue in JavaScript. You might be excited to implement it by yourself. For more modules like these, stay tuned with us. Keep practising!