Skip to main content

Conditional Statements (if, else, switch)

Introduction to Conditional Statements in JavaScript

In programming, we often need to make decisions based on certain conditions. We use conditional statements to perform different actions based on different conditions. In JavaScript, we have several types of conditional statements:

  • if statement
  • else statement
  • else if statement
  • switch statement

The if Statement

The if statement is the most basic of all the control structures in JavaScript. It tells the script to execute a block of code only if a specified condition is true.

Here is the syntax for the if statement:

if (condition) {
// code to be executed if the condition is true
}

Let's look at an example:

let age = 15;

if (age < 18) {
console.log("You are too young to drive.");
}

In this example, the condition is age < 18. If the age is less than 18, the message "You are too young to drive." will be printed in the console.

The else Statement

The else statement is used to specify a block of code to be executed if the condition in the if statement is false.

Here is the syntax for the else statement:

if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}

Let's modify our previous example to include an else statement:

let age = 20;

if (age < 18) {
console.log("You are too young to drive.");
} else {
console.log("You are old enough to drive.");
}

In this example, if the age is less than 18, the message "You are too young to drive." will be printed in the console. If the age is 18 or more, the message "You are old enough to drive." will be printed in the console.

The else if Statement

The else if statement is used to specify a new condition to test if the first condition is false.

Here is the syntax for the else if statement:

if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if both condition1 and condition2 are false
}

Let's look at an example:

let age = 20;

if (age < 18) {
console.log("You are too young to drive.");
} else if (age == 18) {
console.log("Congratulations! You are just old enough to drive.");
} else {
console.log("You are old enough to drive.");
}

In this example, if the age is less than 18, the message "You are too young to drive." will be printed in the console. If the age is exactly 18, the message "Congratulations! You are just old enough to drive." will be printed in the console. If the age is more than 18, the message "You are old enough to drive." will be printed in the console.

The switch Statement

The switch statement is used to perform different actions based on different conditions.

Here is the syntax for the switch statement:

switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

Let's look at an example:

let fruit = "Apple";

switch(fruit) {
case "Banana":
console.log("Banana is good!");
break;
case "Apple":
console.log("I am not a fan of apple.");
break;
default:
console.log("I love all other fruits.");
}

In this example, the variable fruit is checked against the cases inside the switch block. If fruit is "Banana", the message "Banana is good!" will be printed in the console. If fruit is "Apple", the message "I am not a fan of apple." will be printed in the console. If fruit is neither "Banana" nor "Apple", the message "I love all other fruits." will be printed in the console.

Conclusion

Conditional statements are fundamental to every programming language, including JavaScript. They allow your code to make decisions based on specified conditions. By using the if, else, else if, and switch statements, you can control the flow of your program in a variety of ways.