DEV Community

Cover image for Understanding if, else, and else if statements in JavaScript
Mohammad Moiz Ali
Mohammad Moiz Ali

Posted on • Originally published at makstyle119.Medium

Understanding if, else, and else if statements in JavaScript

Conditional statements are an important part of programming, as they allow us to execute different code based on different conditions. In JavaScript, we use if, else, and else if statements to create conditional logic. In this blog post, we will discuss these statements and how to use them in JavaScript.

if Statement:

The if statement is used to execute a block of code if a specified condition is true. The syntax for the if statement is as follows:

if (condition) {
  // code to be executed if condition is true
}
Enter fullscreen mode Exit fullscreen mode

Here, condition is the expression to be evaluated. If condition is true, the code inside the block will be executed.

Let's take a look at an example:

const age = 18;

if (age >= 18) {
  console.log('You are eligible to vote.');
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have used an if statement to check if the age variable is greater than or equal to 18. If it is, the message "You are eligible to vote." will be printed to the console.

else Statement:

The else statement is used to execute a block of code if the condition in the if statement is false. The syntax for the else statement is as follows:

if (condition) {
  // code to be executed if condition is true
} else {
  // code to be executed if condition is false
}
Enter fullscreen mode Exit fullscreen mode

Here, if the condition is true, the code inside the if block will be executed. If the condition is false, the code inside the else block will be executed.

Let's take a look at an example:

const age = 16;

if (age >= 18) {
  console.log('You are eligible to vote.');
} else {
  console.log('You are not eligible to vote.');
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have used an if-else statement to check if the age variable is greater than or equal to 18. If it is, the message "You are eligible to vote." will be printed to the console. If it is not, the message "You are not eligible to vote." will be printed.

else if Statement:

The else if statement is used to execute a block of code if the first condition in the if statement is false and a second condition is true. The syntax for the else if statement is as follows:

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
}
Enter fullscreen mode Exit fullscreen mode

Here, if condition1 is true, the code inside the if block will be executed. If condition1 is false and condition2 is true, the code inside the else if block will be executed. If both condition1 and condition2 are false, the code inside the else block will be executed.

Let's take a look at an example:

const age = 25;

if (age < 18) {
  console.log('You are not eligible to vote.');
} else if (age >= 18 && age <= 65) {
  console.log('You are eligible to vote.');
} else {
  console.log('You are eligible for senior citizen benefits.');
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have used an if-else if-else statement to check if the age variable is less than 18, between 18 and 65, or greater than 65. Depending on the user's age, the appropriate message will be return.

Conclusion:

the if-else statement in JavaScript is an essential tool for controlling the flow of code based on specific conditions. It evaluates a condition and executes a code block if it is true, otherwise it skips to the next condition or the else statement. The else if statement allows for additional conditions to be evaluated, and the else statement is optional and only executed if all preceding conditions are false. if-else statements can be nested for more complex conditions, making them a powerful tool in JavaScript programming.

Top comments (0)

Some comments have been hidden by the post's author - find out more