Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6

The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide the manner of statement execution that is based on the given conditions. The Decision-making statements are decision control statements.

The conditional statements help us to implement various actions based on the various conditions. The conditional statement evaluates the condition before the execution of the instructions. When the developer writes code in a programming language, he needs to perform various actions for various decisions. The conditional statements help us to perform these actions.  

Decision-Making in ES6

Types of Conditional Statement

There are following conditional statements listed below-

  • If statement
  • If…else statement
  • If…else if statement
  • Nested if statement
  • Switch statement

Here, we are going to discuss these conditional statements in detail.

If Statement

In If statement, if the condition is true, then the code is executed otherwise not.

Syntax 

Here is the syntax of If statement.      

if (condition)
 {
           // code will be executed if the condition is true
 } 

In the If condition, if the given condition is true, then the statement within the block will be executed, but if the condition is false, then the code outside the curly braces (if condition) will be executed.

Note- The if statement should be written in lower case characters always. If we use the upper-case character, then there is a chance of error occurrence.

Flowchart of if statement

Decision-Making in ES6

Example

var p = 100;
 if (p > 95)
 {
      Console.log (“p is greater”);
 } 

Output

After the execution of the code, we get the following output.

Decision-Making in ES6

The If…else statement

The if…else statement contains two blocks; if block and the else block. The if…else statement is used when we have two conditions to check. If the condition is true, then the statement (block code) of if condition will be executed, and if the condition is false, then the statement (block code) of else will be executed.

Syntax

if (condition)
 {
        // The code will be executed if the condition is true
 }
 Else
 {
       // The code will be executed if the condition is false    
 }        

Note- In if…else statement, if the condition is true, then the code of if block will be executed, and if the condition is false, then the code of else block will be executed.

Flowchart of if…else statement

Decision-Making in ES6

Example

var r = 50;
 var s = 15;
 if (r < s)
 {
      console.log (“s is greater”);
 }
 Else  
 {
        console.log (“r is greater”);
 } 

Output

After the execution of the code, we get the following output.

Decision-Making in ES6

The if…else if statement

In the if…else statement, the source code of a block will be executed based on the given conditions; otherwise, the code of another block will be executed. The if statement is referred if a condition is true, then execute the block of statements. But if the condition is false, then execute the else statements. When we use else before if statement, then the block code executes when the condition is false.

Syntax

if (condition)
 {
          // The code will be executed if condition1 is true
 }
 else if
 {
           // The code will be executed if condition2 is true 
 }
 else 
 {
                   // The code will be executed if condition1 and condition 2 is false
 }  

Flowchart of if…else if statement

Decision-Making in ES6

Example

var u = 20;
 var u = 30;
 var u = 40;
 if (u > v && u > w)
 {
      console.log (“u is greater”); 
 }
 else if (v > u && v > w)
 {
        console.log (“v is greater”);
 }
 else 
 {
        console.log (“w is greater”); 
 } 

Output

After the execution of the code, we get the following output.

Decision-Making in ES6

The Nested if statement

The term Nested isdefined as a statement within a statement. In the Nested if statement, there is another if statement inside one. We will use nested if statement when we have multiple statements to execute, based on hierarchical condition.

The if statement is executed from top to bottom. If the condition is true, then the code block of if condition will be executed, and another part of the code will be terminated. If all conditions are false, then the code block of else will be executed.

Syntax

if (condition1)
 {
        code block 1;       // code block1 will be executed when the condition1 is true
 if (condition2)
 {
        code block 2;       // code block2 will be executed when the condition2 is true
 }
 else 
 {
        code block 3;       // code block3 will be executed when the condition2 is false
 }
 } 

Example

var a = 11;
 var b = 12;
 var c = 9;
 if (b > a)
 {
        If (b > c)
 { 
        console.log (“b is greater”);
 }
 else
 {
        console.log (“c is greater”);
 }
 }
 else  
 {
          If (a > c)
 {
        console.log (“a is greater”);
 }
 else
 { 
        console.log (“c is greater”);
 }
 } 

Output

After the execution of the code, we get the following output.

Decision-Making in ES6

Switch statement

In a computer programming language, the switch statement is used to check the value of a variable and comparing it with multiple block of codes. The switch statement is a branching statement used for decision making. We mostly use switch statement when all the cases depend upon the value of a single variable.

Sometimes the switch statement is better than an if-else statement. The switch statement is used to execute the block of code that depends upon the multiple cases. The Switch statement contains; Default and Break statement.

The Break statement helps us to terminate a statement sequence in the switch statement. If we use the break, inside the switch statement, then it will stop the execution process in the same block.

The Default statement is used to run a particular code if any condition has no match with the switch cases. It means if the condition of the switch statement does not match with any case, then the statement of default will be executed.

Syntax

switch (expression)
 {
     case 1: 
                   // code for execution 
     break:
      case 2: 
                   // code for execution  
     break:
        .
        .
        .
       case n:
       break:
       default:
                       // code to be executed if any condition does not match  
 } 

Important Points-

  • We can use one or more than one statement inside the switch.
  • The case statements only involve constants. It cannot include expression or a variable.
  • The break and default are the optional blocks.
  • The constant expression and the variable expression must have the same data type.
  • If we use break after every block of code, then the execution flow switch to the next block of code.

Flowchart of the Switch statement

Decision-Making in ES6

Example

var a = 3;
 switch (a)
 {
      Case 1:
           { 
                   Console.log (“Apple”);
 break:
 } 
                Case 2:
           { 
                   Console.log (“Banana”);
 break:
 } 
               Case 3:
           { 
                   Console.log (“mango”);
 break:
 }
               Case 4:
           {  
                   Console.log (“Orange”);
 break:
 }
    default:
 {
          Console.log (“Invalid selection”);
 } 
 } 

Output

After the execution of the code, we get the following output.

Decision-Making in ES6

Advantages of the Switch statement

Some benefits of switch statement are given below-

  • We can compare a number of variable values or conditions with a single switch statement.
  • The Switch statement divides the program into separate modules. It makes error detection easier.
  • We use a switch when many values of the variable are to be compared.