6 – Conditional statements (if, else if, else) (Javascript)

Understanding Conditional Statements

Conditional statements in JavaScript are fundamental constructs that allow you to control the flow of your code based on specific conditions. These statements enable your programs to make decisions and execute different code blocks depending on whether certain conditions are met. In JavaScript, the primary conditional statements are if, else if, and else.

The ‘if’ Statement

The if statement is the most basic form of conditional control in JavaScript. It allows you to execute a block of code if a specified condition is true. If the condition is false, the code block is skipped.


    let temperature = 25;

    if (temperature < 32) {
        console.log("It's freezing outside!");
    }
The ‘else if’ Statement

The else if statement is used when you need to test multiple conditions in a sequential manner. It provides an alternative block of code to execute if the preceding if condition is false, and its own condition is true.


    let score = 75;

    if (score >= 90) {
        console.log("You got an A!");
    } else if (score >= 80) {
        console.log("You got a B.");
    } else if (score >= 70) {
        console.log("You got a C.");
    } else {
        console.log("You need to improve your score.");
    }
The ‘else’ Statement

The else statement is used to specify a code block that should be executed if none of the preceding conditions in an if-else if-else chain is true.


    let age = 15;

    if (age < 18) {
        console.log("You are a minor.");
    } else {
        console.log("You are an adult.");
    }
Nested Conditional Statements

In JavaScript, you can nest conditional statements inside one another to create more complex decision-making structures. This allows you to handle various scenarios based on multiple conditions.


    let temperature = 25;
    let isRaining = false;

    if (temperature < 32) {
        if (isRaining) {
            console.log("It's freezing and raining.");
        } else {
            console.log("It's freezing but not raining.");
        }
    } else {
        console.log("It's not freezing.");
    }
Logical Operators in Conditional Statements

Conditional statements often involve logical operators to combine or modify conditions. JavaScript provides three main logical operators: && (AND), || (OR), and ! (NOT).


    let isSunny = true;
    let isWarm = true;

    if (isSunny && isWarm) {
        console.log("It's a perfect day!");
    }

    if (isSunny || isWarm) {
        console.log("It's either sunny or warm (or both).");
    }

    if (!isSunny) {
        console.log("It's not sunny.");
    }
Conclusion

Conditional statements are crucial for creating dynamic and decision-making JavaScript code. They allow your programs to respond to specific conditions and execute different code blocks accordingly. By mastering if, else if, and else statements, along with logical operators, you gain the ability to build complex and responsive applications.