Premium Javascript Course

JavaScript Conditions

Conditions in JavaScript are used to make decisions in code. Based on the evaluation of a condition, different actions can be taken. This is fundamental to control the flow of your program.

1. If Statement

The if statement is used to execute a block of code if the specified condition evaluates to true.

Syntax:


if (condition) {
    // code to be executed if condition is true
}
        

Example:


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

In this example, the code inside the if block is executed because the condition (age >= 18) evaluates to true.

2. If-Else Statement

The if-else statement allows you to run one block of code if the condition is true and another if the condition is false.

Syntax:


if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}
        

Example:


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

Here, since the condition (age >= 18) evaluates to false, the code inside the else block is executed.

3. If-Else If-Else Statement

The if-else if-else statement is used to check multiple conditions. If the first condition is false, the next condition is evaluated.

Syntax:


if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition2 is true
} else {
    // code to be executed if none of the conditions are true
}
        

Example:


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

In this example, the else if block is executed because the grade is 85, which satisfies the condition (grade >= 80) but not the first one.

4. Switch Statement

The switch statement is used to evaluate an expression, matching its value to a case clause. If there is a match, the corresponding block of code is executed. If there is no match, the default block is executed.

Syntax:


switch (expression) {
    case value1:
        // code to be executed if expression === value1
        break;
    case value2:
        // code to be executed if expression === value2
        break;
    default:
        // code to be executed if no cases match
}
        

Example:


let day = 3;
switch (day) {
    case 1:
        console.log("Monday");
        break;
    case 2:
        console.log("Tuesday");
        break;
    case 3:
        console.log("Wednesday");
        break;
    default:
        console.log("Invalid day");
}
        

In this case, the expression (day) matches the value 3, so the code inside case 3 is executed, printing Wednesday.

5. Ternary Operator

The ternary operator is a shorthand way of writing an if-else statement.

Syntax:


condition ? expression1 : expression2;
        

Example:


let age = 20;
let status = (age >= 18) ? "Adult" : "Minor";
console.log(status); // Adult
        

Conclusion

JavaScript conditions are a crucial part of controlling the flow of your program. Whether you're checking a single condition with if, or handling multiple possible conditions with if-else or switch, understanding these control structures will allow you to write dynamic and interactive applications.