Conditional expressions in JavaScript

In this tutorial, I am going to explain conditional expression in JavaScript.

There are a lot of cases, where we can use conditional expressions depending upon our needs.

Here are some examples of scenarios where we can use conditional expressions.

  • Suppose we want to check if a given value is correct or not. Then we can use these expressions.
  • If we want to take different actions for different situations we can use these expressions too.

Sometimes, we want a block of code to be executed only when a condition is met. Suppose only 18+ aged people are eligible for voting. Now, you asked a user to enter his/her age and he/she enters 12. So you can give a message to him/her that he/she is not eligible for voting. Similarly, you can give a message to a 26 years old man that he is eligible for voting.

We can also set multiple actions for multiple conditions as well.

Types of conditional statements:

  1. if statement
  2. if...else statement
  3. if...else if statement
  4. Switch case

if statement

For if statement if the condition is true then it will execute otherwise it will not execute.

age = prompt("Enter your age");
        if (age > 18) {
            console.log("You are eligible for voting");
        }

Output

Enter your age> 78
You are eligible for voting

In the above program, if the age is greater than 18 then the condition is true and the block of code will execute. As you can see I have given 78 as input which is greater than 18 so the condition is true and gave the output “You are eligible for voting”.

  • If you want that 18 and more than 18 years old are eligible then you can use the >= (age>=18) operator to compare.
  • For only 18 years old you can use the == (age==18)  operator.

if...else statement

The if statement has an optional else statement. If the condition is true, the code inside the if will execute otherwise code inside else will execute.

age = prompt("Enter your age"); 
if (age > 18) {
  console.log("You are eligible for voting");
}
else{
  console.log("You are not eligible for voting")
}

Output

Enter your age> 12
You are not eligible for voting

In the above program, I have given 12 as input which is not true because 12 is less than 18 here the condition is false so the statement in the else block is executed and gave the output ‘You are not eligible for voting’.

if...else if statement

Sometimes, we might have multiple conditions and want to check our conditions one by one until one matches. We can use the if...else if statement for this.

age = prompt("Enter your age"); 
if (age < 0) {                   //condition1
  console.log("Invalid age");
}
else if (age < 10){              //condition2
  console.log("You are a kid")
}
else if (age < 18 && age > 9){   //condition3
  console.log("You can think about voting")
}
else{                            //condition4
  console.log("You are eligible for voting")
}

In the above code, all the conditions are one nested if, else if, else statement. It can be multiple else if 1, 2, or even 100 but only one will execute among all. If condition1 is true and printed then the remaining conditions will be skipped.

Now, if condition1 is false then it will go for condition2 if condition2 is false it will go for condition3 if condition3 is also false it will go for condition4. As much as else if you give, it will check all of them and finally, it will go inside else. If none of the conditions turns true then it will execute the final statement else.

Output

Enter your age> 9
You are a kid

I just put 9 as output and condition2 turns true and gave me the output ‘You are a kid’. Here condition2 is true, so the remaining conditions were skipped.

Switch case

The switch statement is the basic control structure used to perform different actions based on different conditions.

const bike = 5; 
    switch (bike) {
        case 4:
            console.log("The number of bike is 56");
            break;

        case 5:
            console.log("The number of bike is 62");
            break;

        case 7:
            console.log("The number of bike is 65");
            break;

        case 12:
            console.log("The number of bike is 78");
            break;
    
        default:
            console.log("The number of bike is none of 56, 62, 65, 78");
            break;
    }

In the above code, I have declared a variable named bike and  5 as the value of that variable. Then I have written switch (bike)now the value of bike can be anything and you can evaluate it once. Then it will compare the value of bike with every case value. Now if the case matches the block of code inside it will execute.

Output

The number of bike is 62

As you can see, bike value is 5, it matches with the case 5 and gave output ‘The number of bike is 62‘.

Leave a Reply

Your email address will not be published. Required fields are marked *