if, else, else…if statement in JavaScript

In this article, we'll be taking about the conditional statements and its types. You can read more about conditional statements in this article.

There are three types of conditional statements in JavaScript:

Types of Conditional Statements in JavaScript:

1.if statement

In JavaScript, the "if" statement is used to execute a block of code based on a specified condition. It allows you to create conditional logic in your programs.

The basic syntax of the "if" statement is as follows:

if (condition) {
    // Code block to execute if the condition is true
}

The "condition" is an expression that evaluates to either true or false. If the condition is true, the code block within the curly braces will be executed. If the condition is false, the code block will be skipped, and the program will continue to the next statement after the "if" block.

For Example:

var age = 20;
if (age >= 18) {
    console.log("You are an adult.");
}

Inference : In this example, the condition age >= 18 checks if the variable "age" is greater than or equal to 18. If the condition is true, the message "You are an adult" will be printed to the console.

The "if" statement is a fundamental building block in JavaScript that allows you to make decisions and control the flow of your code based on specific conditions.

2. else statement

In JavaScript, the "else" statement is used in conjunction with the "if" statement to specify an alternative code block to execute when the condition in the "if" statement evaluates to false. The "else" statement is optional and allows you to provide an alternative path of execution.

The basic syntax is as follows:

if (condition) {
    // Code block to execute if the condition is true
} else {
    // Code block to execute if the condition is false
}

For Example:

var age = 15;
if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are a minor.");
}

Inference : In this example, the condition age >= 18 checks if the variable "age" is greater than or equal to 18. If the condition is true, the message "You are an adult" will be printed to the console. If the condition is false, the "else" block will be executed, and the message "You are a minor" will be printed.

It's important to note that the "else" statement is associated with the nearest "if" statement that precedes it. If there are multiple "if" statements, each "else" statement corresponds to its nearest preceding "if" statement.

Using the "else" statement provides an alternative code path when the condition in the "if" statement is false. It allows you to create more flexible and responsive code that can handle different scenarios based on specific conditions.

3. else…if statement

In JavaScript, the "else if" statement is used in conjunction with the "if" statement to handle multiple conditions. It allows you to specify additional conditions to check if the preceding "if" statement evaluates to false. The "else if" statement provides an alternative code block to execute when the subsequent condition is true.

The basic syntax is as follows:

if (condition1) {
    // Code block to execute if condition1 is true
} else if (condition2) {
    // Code block to execute if condition2 is true
} else {
    // Code block to execute if all conditions are false
}

For Example:

var num = 0;
if (num > 0) {
    console.log("Number is positive.");
} else if (num < 0) {
    console.log("Number is negative.");
} else {
    console.log("Number is zero.");
}

Inference : In this example, if "num" is greater than 0, the message "Number is positive" will be printed. If the first condition is false, the program will evaluate the next condition in the "else if" statement. If "num" is less than 0, the message "Number is negative" will be printed. If both conditions are false, the "else" block will be executed, and the message "Number is zero" will be printed.

The "else if" statement allows you to handle multiple conditions and provides a way to create more complex decision-making logic in your JavaScript programs. It enables you to specify different code blocks to execute based on various conditions.

Conclusion

In this article, you've read about the types of conditional statements i.e., if, else, else…if statements and when to use each. Examples are also been explained to have a clear understanding. You can use any of these as per the requirements. Now, let's move ahead and read about the switch cases which are also a type of conditional statements.


Learn via Video Course

Javascript(English) Logo

Javascript(English)

72966

3 hrs