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.

Related Topics

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

5 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.