DEV Community

Cover image for Conditional Statements in JS
Rakshambika
Rakshambika

Posted on

Conditional Statements in JS

What is Conditional Statements?

  • Conditional Statements are used to execute a different block of code based on the conditions.
  • Conditional Statements are also known as Decision-making statements.

Types of Conditional Statements:

        1. if statement
        2. if else statement
        3. else if statement
        4. Nested statement
        5. Switch statement
Enter fullscreen mode Exit fullscreen mode

If statement:

It executes the block of code if the condition is satisfied, otherwise nothing is executed.

Example:

// if statement

let age=20;

if(age>18)
{
    console.log("Eligible to vote");
}
Enter fullscreen mode Exit fullscreen mode

Output:

Eligible to vote
Enter fullscreen mode Exit fullscreen mode

In the above example, the age is 20 and the condition is (age>18). So here the condition is true and the output is Eligible to vote . If the age is less than 18, then nothing will be printed.


if else statement:

If the condition is satisfied then it executes the block of code inside the if block, if the condition is not satisfied then it executes the block of code inside the else block.

Example:

 let age =15;

    if(age>20)
    {
        console.log("Eligible to vote");
    }

    else
    {
        console.log("Not Eligible to vote");
    }
Enter fullscreen mode Exit fullscreen mode

Output:

Not Eligible to vote
Enter fullscreen mode Exit fullscreen mode

In the above example, the age is 15 and the condition is (age>18). Here the condition is false and the output is Not Eligible to vote . If the age is greater than 18, then it will execute the block of code inside the if block.


else if statement:

Using if else statement we can only compare one condition. If we want to check multiple conditions at that time we use else if statement.

Example:

If we are calculating the grade for every subjects based on our marks. we need check multiple conditions because different marks can correspond to different grades.

let mark=80;

if(mark>90 && mark<=100)
{
    console.log("O grade");
}

else if(mark>80 && mark<=90)
{
    console.log("A+ grade");
}

else if(mark>70 && mark<=80)
{
    console.log("A grade");
}

else if(mark>60 && mark<=70)
{
    console.log("B+ grade");
}

else if(mark>50 && mark<=60)
{
    console.log("B grade");
}

else if(mark>40 && mark<=50)
{
    console.log("C grade");
}

else if(mark>0 && mark<=40)
{
    console.log("Fail");
}

else
{
    console.log("Enter the Valid mark");
}
Enter fullscreen mode Exit fullscreen mode

Output:

A grade
Enter fullscreen mode Exit fullscreen mode

Nested Statement:

A nested if statement is an if statement placed inside another if statement.

It is used when we need to check one condition inside another condition.

Example:

// Find the largest Number

let a=90 , b=180 , c=190;

if(a>b)
{
    if(a>c)
    {
        console.log("A is greater than B and C");
    }

    else
    {
        console.log("C is greater than A and B");
    }

}

else
   {
    if(b>c)
    {
         console.log("B is greater than A and C");
    }

    else
    {
        console.log("C is greater than A and B");
    }
   }


Enter fullscreen mode Exit fullscreen mode

Output:

C is greater than A and B
Enter fullscreen mode Exit fullscreen mode

In the above example, we have three numbers: a = 90, b = 180, and c = 190.

First, the condition a > b is checked. Since 90 > 180 is false, the else block is executed.

Inside the else block, the condition b > c is checked. Since 180 > 190 is false, the nested else block is executed.

Therefore, c is the largest number, and the output is:

C is greater than A and B.


Switch statement:

The switch statement is a conditional statement used to execute one block of code from multiple possible options based on the value of an expression.

It is useful when we need to compare one value with multiple possible values.

Syntax:

switch (expression) {
    case value1:
        // code
        break;

    case value2:
        // code
        break;

    case value3:
        // code
        break;

    default:
        // code
}
Enter fullscreen mode Exit fullscreen mode

Example:

// Switch statement

let day = 4;

switch (day) {

    case 1:
        console.log("Sunday");
        break;

    case 2:
        console.log("Monday");
        break;

    case 3:
        console.log("Tuesday");
        break;

    case 4:
        console.log("Wednesday");
        break;

    case 5:
        console.log("Thursday");
        break;

    case 6:
        console.log("Friday");
        break;

    case 7:
        console.log("Saturday");
        break;

    default:
        console.log("Invalid day");
}
Enter fullscreen mode Exit fullscreen mode

Output:

Wednesday
Enter fullscreen mode Exit fullscreen mode

The switch statement compares day with each case:

3 is not equal to 1
3 is not equal to 2
3 is equal to 3

Therefore, the code inside case 3 is executed, and "Wednesday" is printed.

The break statement stops the execution of the switch statement after the matching case is executed.

The default block is executed when none of the cases match.

Top comments (0)