Selection Statements in JavaScript

Introduction

 
Suppose there is a web page in our shopping web site that accepts a price range from the users. When the users provide a price range, the items or products that lie within the specified price range are displayed. Such functionality and logic can be incorporated using a select statement in the script.
 
A selection statement uses a condition to select, or determine, the statement that is to be executed. These statements help us to make decisions and change the flow of execution of the statements. In the JavaScript, there are three selection statements as in the following:
  1. if
  2. if....else
  3. switch
Let's learn more about these statements.
 

The if Statement

 
If the statement is one of the most basic and simplest controls flow statements. We can use the if statement when we want to execute a group of one or more script statements only when a particular condition is met.
 
Syntax
  1. if(condition)  
  2. {  
  3. Statement 1  
In the preceding syntax, if the JavaScript keyword that signifies an if statement and contains a condition, that needs to be evaluated to true, then the script statement, represented by statement 1, enclosed within the curly braces, is executed. If the condition evaluates to false, then the statement enclosed within the curly braces is skipped and the statement immediately after closing curly brace (}) is executed.
 
Let's understand a simple example of a if statement as in the following:
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <title>Using if Statement</title>  
  5. </head>  
  6. <body>  
  7.     <h1>  
  8.         Using the if Statement in the script  
  9.     </h1>  
  10.     <script type="text/javascript">  
  11.                                                     var Number = 45;  
  12.         if ((Number % 2) != 0) {  
  13.             document.write(Number + " is an odd number");  
  14.         }  
  15.         document.write("<BR/>Thank you!");  
  16.     </script>  
  17. </body>  
  18. </html> 
Output
 
JavaScript1.jpg
 

The if.....else statement

 
As we know, the if statement allows us to execute a set of statements only when a particular condition is true. However, if we want to execute another set of statements when the condition is false, then we can use the if....else statement.
 
Syntax
  1. if(condition)  
  2. {  
  3. Statement 1  
  4. }  
  5. else  
  6. {  
  7. Statement 2  
In the given syntax, the if and else keywords signify the if...else statement. The condition of the if....else statement is enclosed within parentheses. If the condition is true, then the group of statements represented by statement 1 enclosed within the first curly braces is executed. If the condition is false, then the statement 1 is skipped and the group of statements, represented by statement 2 of the else block is executed.
 
Let's try a simple if else statement as given below:
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <title>if...else Statement</title>  
  5. </head>  
  6. <body>  
  7.     <h1>  
  8.         Using the if...else Statement in the Script</h1>  
  9.     <script type="text/javascript">  
  10.                                                     var Number = 44;  
  11.         if ((Number % 2) != 0) {  
  12.             document.write(Number + " is an odd number");  
  13.         }  
  14.         else {  
  15.             document.write(Number + " is an even  number");  
  16.         }  
  17.         document.write("<BR/>Thank you!");  
  18.     </script>  
  19. </body>  
  20. </html> 
Output
 
JavaScript2.jpg
 

The switch statement

 
A switch statement selects a particular group of statements to be executed among several other groups of statements. The group of statements that are to be executed is selected on the basis of a numeric or string expression.
 
Syntax
  1. switch(expression)  
  2. {  
  3. case value1:statement 1  
  4. break;  
  5. case value2:statement 2  
  6. break;  
  7. case value3: statement 3  
  8. break;  
  9. default:statement_default  
  10. break;  
In the given syntax, the switch case, and break are JavaScript keywords. The switch keyword indicates the switch statement. In a switch statement, the expression that is to be evaluated is specified within parentheses. This expression is checked against each of the case values specified in the case statements. If any of the case values match the value of the expression, the group of statements (statement 1, statement 2 or statement 3) specified in the respective case statement is executed.
 
If none of the case values matches the value of the expression, then the default statement, specified by the default keyword, is executed. The default statement is generally placed at the end of the switch statement; however, we can place it anywhere within the switch statement.
 
Let's try a simple example of the switch statement as given below:
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4.     <title>Using switch Statement</title>  
  5. </head>  
  6. <body>  
  7.     <h1>  
  8.         Using switch Statement in the script</h1>  
  9.     <script type="text/javascript">  
  10.                                                     var letter = "I";  
  11.         switch (letter) {  
  12.             default: document.write("consonant");  
  13.                 break;  
  14.             case "A": document.write("A is a vowel");  
  15.                 break;  
  16.             case "E": document.write("E is a vowel");  
  17.                 break;  
  18.             case "I": document.write("I is a vowel");  
  19.                 break;  
  20.             case "O": document.write("O is a vowel");  
  21.                 break;  
  22.             case "U": document.write("U is a vowel");  
  23.                 break;  
  24.         }  
  25.         document.write("<BR/>Thank You!");  
  26.     </script>  
  27. </body>  
  28. </html> 
Output
 
JavaScript3.jpg


Foreantech
Foreantech - A complete online solution company.