If-else Statements in JavaScript


Many times in our life, not only we choose to do something if a condition is met, but also choose to do something different if that condition is not met. For example:

If I'm tired:
    I take a nap
Otherwise:
    I start coding

If-else Statements:

When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen.

With an else statement, we can have an alternate block of code to be executed. For example:

let amTired = false;
if(amTired) {
    console.log("I take a nap");
}
else {
    console.log("I start coding");
}

The code above prints "I start coding" since the expression inside the if evaluates to false and so JavaScript will enter the else statement and execute the code inside it.

An else statement cannot exist without a corresponding if statement. This combination is refered to as an if-else statement.


Assignment
Follow the Coding Tutorial and let's practice with if-else statements!


Hint
Look at the examples above if you get stuck.


Introduction

If-else statements are a fundamental concept in programming that allow you to execute different blocks of code based on certain conditions. This is crucial for making decisions in your code, enabling dynamic and responsive behavior in your applications. Common scenarios where if-else statements are useful include form validation, user authentication, and game logic.

Understanding the Basics

At its core, an if-else statement evaluates a condition and executes a block of code if the condition is true. If the condition is false, it executes an alternate block of code. Understanding this basic structure is essential before moving on to more complex logic.

let isRaining = true;
if (isRaining) {
    console.log("Take an umbrella");
} else {
    console.log("No need for an umbrella");
}

In this example, the message "Take an umbrella" will be printed because the condition isRaining is true.

Main Concepts

The key concepts in if-else statements include:

Let's see another example:

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

Here, the message "You are an adult" will be printed because the condition age >= 18 is true.

Examples and Use Cases

Let's explore some more examples to understand the versatility of if-else statements:

let score = 85;
if (score >= 90) {
    console.log("Grade: A");
} else if (score >= 80) {
    console.log("Grade: B");
} else if (score >= 70) {
    console.log("Grade: C");
} else {
    console.log("Grade: F");
}

In this example, the message "Grade: B" will be printed because the score is 85, which falls in the range for a B grade.

Common Pitfalls and Best Practices

Common mistakes include:

Best practices include:

Advanced Techniques

Advanced techniques include nested if-else statements and using logical operators to combine multiple conditions:

let temperature = 30;
let isSunny = true;
if (temperature > 25 && isSunny) {
    console.log("It's a great day for the beach!");
} else {
    console.log("Maybe stay indoors.");
}

In this example, the message "It's a great day for the beach!" will be printed because both conditions are true.

Code Implementation

Here is a well-commented code snippet demonstrating the use of if-else statements:

let userLoggedIn = true;
let userRole = "admin";

if (userLoggedIn) {
    // Check the role of the user
    if (userRole === "admin") {
        console.log("Welcome, Admin!");
    } else {
        console.log("Welcome, User!");
    }
} else {
    console.log("Please log in.");
}

This code checks if a user is logged in and then checks their role to display an appropriate message.

Debugging and Testing

Debugging tips include:

Testing tips include:

function isAdult(age) {
    return age >= 18;
}

// Test cases
console.log(isAdult(20)); // true
console.log(isAdult(17)); // false

Thinking and Problem-Solving Tips

When approaching problems involving if-else statements:

Conclusion

If-else statements are a powerful tool in programming that allow you to make decisions in your code. Mastering this concept is essential for writing dynamic and responsive applications. Practice regularly to improve your understanding and proficiency.

Additional Resources

For further reading and practice, check out these resources: