Open In App

ES6 Decision Making

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

ES6 Decision Making in programming is needed to make the logical decision for doing some operations. In ES6, decision making can be made using the following conditional statements:

Note: The following points should be considered while using the if, if-else or if-else ladder.

  • Curly braces {} should be used if more than one statement has to be executed within if or else block.
  • The condition evaluated within if will either be true or false.

if statement: It is used if some statement has to be executed based on single condition. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then the block of statement is executed otherwise not.

Syntax:

if (condition)
{
    // Set of statement which has
    // to be executed if condition
    // is satisfied
}

Flowchart:
if - ES6
Example 1:




<script>
let a = 10;
  
if (a > 0) {
    console.log(a + " is a natural number.");
}
</script>


Output:

10 is a natural number.

Example 2:




<script>
let a = 10;
let b = 20;
  
if (a > b) {
    console.log("a is greater than b");
}
  
// Independent Statement
console.log("Hello World");
</script>


Output:

Hello World

if-else statement: It is used when some set of code has to be executed based on some given conditions otherwise another set of code has to be executed. The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false then else block executed. We can use the else statement with if statement to execute a block of code when the condition is false.

Syntax:

if (condition) {

    // Set of statement which has
    // to be executed if condition 
    // is satisfied
} 
else {

    // Set of statement which has
    // to be executed if condition 
    // is not satisfied
}

Flowchart:
if-else

Example:




<script>
    let a = 10;
    let b = 20;
  
    if (a > b) {
        console.log("a is greater than b");
    }
    else {
        console.log("b is greater than a");
    }
</script>                    


Output:

b is greater than a

if-else ladder: It is used when some set of the statement has to executed based on hierarchical conditions. A user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax:

if (condition-1) {
    if (condition-2) {

        // statements - if condition-1 and
        // condition-2 both are satisfied
    }

    // statements - condition-1 is satisfied
}

Example 1:




<script>
let a = 10;
let b = 20;
let c = 8;
if (b > a) {
    if (b > c) {
        console.log("b is greatest");
    } else {
        console.log("c is greatest");
    }
} else {
    if (a > c) {
        console.log("a is greatest");
    } else {
        console.log("c is greatest");
    }
}
</script>


Output:

b is greatest

switch-case: It is used when multiple condition has to be evaluated based on single variable expression.
Syntax:

switch(expression) {
    case_I :
       // statements for case_I
       break;

    case_II :
       // statements for case_II
       break;

    case_n :
       // statements for case_n
       break;

    default :
       // statements for default
}

Note: Following point must be taken into the consideration while using switch-case

  • The switch statement accepts n number of cases.
  • Cases are case sensitive.
  • Default has the least priority (If any case does not matches with the given variable expression then only default will be executed irrespective of its position) and default is optional.
  • The cases must be of constant and unique. It can not be a variable or expression.
  • The execution will flow through each cases if break is missing in the satisfied case, until a break is not encountered.

Flowchart:
switch-case

Example 1:




<script>
let day = 4;
switch(day) 
{
    case 0:
        console.log("Monday");
        break;
    case 1:
        console.log("Tuesday");
        break;
    case 2:
        console.log("Wednesday");
        break;
    case 3:
        console.log("Thursday");
        break;
    case 4:
        console.log("Friday");
        break;
    case 5:
        console.log("Saturday");
        break;
    case 6:
        console.log("Sunday");
        break;
}
</script>


Output:

Friday

Example 2:




<script>
let name = "Jhon";
  
switch(name) {
    default :
        console.log("No data found");
        break;
         // should be used to avoid fall through other cases below
         // if no case matches the variable expression
  
    case "jhon" :
        console.log("I am small");
        break;
    case "Jhon" :
        console.log("I am original");
        break;
    case "JhoN" :
        console.log("I am modified");
        break;
}
</script>


Output:

I am original

Example 3:




<script>
let a = 10;
  
switch(a) {
    default:
        console.log("I am default");
    case 12:
        console.log("I am 12");
    case 13:
        console.log("I am 13");
    case "10":
        console.log("I am string 10");
    case 15:
        console.log("I am 15");
        break;
    case 16:
        console.log("I am 16");
}
</script>


Output:

I am default
I am 12
I am 13
I am string 10
I am 15

Ternary Operator: It is also an if-else shorthand method. It takes three argument as input, condition, statement if condition satisfied, and statement if condition not satisfied.

Syntax:

(condition expression) ? statement for true : statement for false;

Example 1:




<script>
let a = 10;
(a == 10) ? console.log("geeksforgeeks") : console.log("you are wrong");
</script>


Output:

geeksforgeeks

Example 2:




<script>
let a = 10;
let b = 20;
let c = 15;
let max = (a > b) ? (a > c) ? a : c : (b > c) ? b : c;
console.log(max);
</script>


Output:

20


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads