DEV Community

Mahalakshmi C
Mahalakshmi C

Posted on

Conditional Statement

Conditional Statements in JavaScript

Conditional statements in JavaScript are used to make decisions based on a condition. They allow a program to execute different blocks of code depending on whether a condition is true or false.

For example, we can check whether a shopping cart has items before allowing a user to proceed to checkout.

JavaScript provides different conditional statements such as if, if...else, else if, nested if, and switch.

1. if Statement

The if statement is used when we want to execute a block of code only when a particular condition is true.

The syntax is:

if (condition) {
    // code to execute
}
Enter fullscreen mode Exit fullscreen mode

For example:

let cartItems = 3;

if (cartItems > 0) {
    console.log("Proceed to checkout");
}
Enter fullscreen mode Exit fullscreen mode

Output:

Proceed to checkout
Enter fullscreen mode Exit fullscreen mode

Here, the condition cartItems > 0 is true, so the code inside the if block is executed.

If the condition is false, the code inside the if block will not execute.

2. if...else Statement

The if...else statement is used when we have two possible outcomes. If the condition is true, the if block is executed. If the condition is false, the else block is executed.

For example:

let password = "12345";

if (password.length >= 8) {
    console.log("Password is strong");
} else {
    console.log("Password is too short");
}
Enter fullscreen mode Exit fullscreen mode

Output:

Password is too short
Enter fullscreen mode Exit fullscreen mode

Here, the password contains fewer than 8 characters, so the else block is executed.

3. else if Statement

The else if statement is used when we need to check multiple conditions. JavaScript checks the conditions from top to bottom and executes the first condition that is true.

For example:

let temperature = 32;

if (temperature >= 35) {
    console.log("Very hot");
} else if (temperature >= 30) {
    console.log("Hot");
} else if (temperature >= 20) {
    console.log("Normal");
} else {
    console.log("Cold");
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hot
Enter fullscreen mode Exit fullscreen mode

Here, temperature >= 35 is false, but temperature >= 30 is true, so "Hot" is printed.

Once JavaScript finds a true condition, it does not check the remaining else if conditions.

4. Nested if Statement

A nested if statement means placing one if statement inside another if statement. It is useful when one condition depends on another condition.

For example:

let isLoggedIn = true;
let isAdmin = true;

if (isLoggedIn) {
    if (isAdmin) {
        console.log("Welcome to the admin dashboard");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to the admin dashboard
Enter fullscreen mode Exit fullscreen mode

Here, JavaScript first checks whether the user is logged in. If that condition is true, it then checks whether the user is an admin.

5. switch Statement

The switch statement is used when we want to compare one value with multiple possible values. It can be a cleaner alternative to using multiple else if statements in some situations.

The syntax is:

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

    case value2:
        // code
        break;

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

For example:

let paymentMethod = "UPI";

switch (paymentMethod) {
    case "UPI":
        console.log("Payment through UPI");
        break;

    case "Card":
        console.log("Payment through Card");
        break;

    case "Cash":
        console.log("Payment through Cash");
        break;

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

Output:

Payment through UPI
Enter fullscreen mode Exit fullscreen mode

Here, the value of paymentMethod is "UPI", so JavaScript matches it with case "UPI" and executes that block.

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

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

if...else vs switch

if...else switch
Used for conditions and comparisons Used mainly to compare one expression with different values
Can check ranges such as price > 1000 Usually checks specific matching values
Suitable for complex conditions Suitable for multiple fixed choices
Uses if, else if, and else Uses case, break, and default

Top comments (0)