Block, Break, Continue and Return Statements in JavaScript

Introduction

 
In the previous chapter, we learned about looping statements in JavaScript and how to use these statements in JavaScript with example programs.
  
In this chapter, we will learn about the use of a JavaScript block, continue, break, and return Statements that are very important for starting with the fundamentals of JavaScript.
 

JavaScript block statement

 
JavaScript block statements are the curly braces ( { } ) you see everywhere. They are used to establish a code that is to be compartmentalized into a specific function or statement.
 
The lines of code inside of a block statement is often intended to represent that they are part of that block statement.
 
The following are some examples showing the curly braces establishing blocks of compartmentalized code.
  1. <script>  
  2.     if (3 < 5)  
  3.     {  
  4.         // code for this block statement goes in here  
  5.     }  
  6.     for (var i = 0; i < 10; i++)  
  7.     {  
  8.         // code for this block statement goes in here  
  9.     }  
  10.     function myFunction()  
  11.     {  
  12.         // code for this block statement goes in here  
  13.     }  
  14. </script> 
TIP: You may come across code that processes just fine without the block statements in place (no curly brace nesting).
 
This is because JavaScript will execute all lines of code as one statement until it hits a semicolon( ; ), which is the statement break symbol in JavaScript and many other languages.
  1. <script>  
  2.     if(3 < 5)  
  3.         document.write("EXPRESSION RETURNS TRUE")  
  4.     else  
  5.         document.write("EXPRESSION RETURNS FALSE");  
  6. </script> 
block-statement-in-javascript.png
 

JavaScript break statement   

 
The JavaScript break statement is used to terminate a loop, switch or label statement from further processing.
 
Apply it when you want to force one of those types of statements to stop processing.
 
Example of terminating a loop using break
  1. <script>  
  2.     for (i = 0; i < 20; i++)  
  3.     {  
  4.         if (i >= 5)  
  5.         {  
  6.             break;  
  7.         }  
  8.         document.write("Pass index " + i + " of the loop<br />");  
  9.     }  
  10. </script> 
terminating-loop-statemnet-in-javascript.png
 
Using break above we were able to make a loop that has a ceiling of 20 passes, and force it to stop processing after 5 passes.
 
Example of terminating a switch statement using break
 
If the break is not applied to cases of a switch statement, the switch statement will continue processing until its end.
  1. <script>  
  2.     var browser = "Chrome";  
  3.     switch (browser)  
  4.     {  
  5.         case "IE":  
  6.             document.write("Internet Explorer makes life hard for webmasters.");  
  7.             break;  
  8.         case "Chrome":  
  9.             document.write("Chrome confirms to popular browser standards.");  
  10.             break;  
  11.         case "Firefox":  
  12.             document.write("Firefox is bulky but developer friendly.");  
  13.             break;  
  14.         default:  
  15.             document.write("No information for browser: " + browser);  
  16.     }  
  17. </script> 
terminating-switch-statement-in-javscript.png
 

JavaScript continue statement 

 
The JavaScript continue statement is used to bypass specified iterations of a loop so that the code in the loop statement does not execute for the specified iterations, and moves on to the next.
  1. <script>  
  2.     for (i = 0; i < 20; i++)  
  3.     {  
  4.         if (i < 10)  
  5.         {  
  6.             continue;  
  7.         }  
  8.         document.write("Pass index " + i + " of the loop<br />");  
  9.     }  
  10. </script> 
continue-statement-in-javascript.png
 

JavaScript return statement

 
The JavaScript return statement is used to return a value from a function. It terminates the function from further executing code, and returns a value to the caller.
  1. <script>  
  2.     function addNums(v1, v2)  
  3.     {  
  4.       return v1 + v2;  
  5.     }  
  6.     var sum = addNums(2, 3);  
  7.     document.write(sum);  
  8. </script> 
return-statement-in-javascript.png
 

Summary

 
In this chapter, we learned about the use of a JavaScript block, continue, break, and return Statements and how to use these statements in JavaScript with example programs.
Author
Next » Functions in JavaScript