Conditional Statements

Conditional statements allow you to execute different blocks of code based on specific conditions. The most common conditional statements in JavaScript are if, else if, and else.

If Statement

The if statement evaluates a condition and executes a block of code if the condition is true.

let temperature = 30;

if (temperature > 25) {
    console.log("It's a hot day!");
}

If-Else Statement

The if-else statement provides an alternative block of code to execute if the condition is false.

let temperature = 20;

if (temperature > 25) {
    console.log("It's a hot day!");
} else {
    console.log("It's a pleasant day!");
}

Else If Statement

The else if statement allows you to check multiple conditions.

let temperature = 15;

if (temperature > 25) {
    console.log("It's a hot day!");
} else if (temperature < 15) {
    console.log("It's a cold day!");
} else {
    console.log("It's a moderate day!");
}

Switch Statement

The switch statement is another way to handle multiple conditions based on the value of a variable. It is often cleaner and more readable than using multiple if-else statements.

let fruit = "apple";

switch (fruit) {
    case "banana":
        console.log("Banana is yellow.");
        break;
    case "apple":
        console.log("Apple is red.");
        break;
    case "grape":
        console.log("Grape is purple.");
        break;
    default:
        console.log("Unknown fruit.");
}

Looping Statements

Loops allow you to execute a block of code multiple times. JavaScript provides several types of loops, including for, while, and do...while.

For Loop

The for loop is typically used when the number of iterations is known.

for (let i = 0; i < 5; i++) {
    console.log("Iteration " + i);
}

While Loop

The while loop continues to execute as long as a specified condition is true.

let count = 0;

while (count < 5) {
    console.log("Count is: " + count);
    count++;
}

Do...While Loop

The do...while loop is similar to the while loop, but it guarantees that the block of code will execute at least once.

let count = 0;

do {
    console.log("Count is: " + count);
    count++;
} while (count < 5);

Best Practices

  1. Use Curly Braces: Always use curly braces {} for blocks of code, even for single statements. This improves readability and reduces errors.
    if (condition) {
        // Code block
    }
  1. Avoid Deep Nesting: Deeply nested conditional statements can make code difficult to read. Consider using early returns or restructuring your logic.
  1. Use switch for Multiple Conditions: When checking a single variable against multiple values, prefer switch for better readability.
  1. Use Descriptive Variable Names: Clear variable names make it easier to understand the purpose of your conditions.

Comparison of Control Flow Structures

StructureUse CaseSyntax Example
ifSingle condition checkif (condition) { ... }
if-elseTwo possible pathsif (condition) { ... } else { ... }
else ifMultiple conditionsif (cond1) { ... } else if (cond2) { ... }
switchMultiple values for a single variableswitch (variable) { case value: ... }
forKnown number of iterationsfor (let i = 0; i < limit; i++) { ... }
whileUnknown number of iterations, condition-basedwhile (condition) { ... }
do...whileAt least one execution guaranteeddo { ... } while (condition);

Conclusion

Mastering control flow in JavaScript is essential for building robust applications. By understanding and effectively using conditional statements and loops, developers can create dynamic and responsive code that reacts to different conditions and user inputs. Remember to follow best practices to enhance code readability and maintainability.

Learn more with useful resources: