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.      

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

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

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

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

Flowchart of if…else if statement

Decision-Making in ES6

Example

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

Example

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

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

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.

Pin It on Pinterest

Share This