Prev Page Next Page
Top

The if Statement

➢Use the if statement to specify a block of JavaScript code to be executed if a condition is true.

EXAMPLE:

if (hour < 18) {
    greeting = "Good day";
}

The else Statement

➢Use the else statement to specify a block of code to be executed if the condition is false.

EXAMPLE:

if (hour < 18) {
    greeting = "Good day";
} else {
    greeting = "Good evening";
}

The else if Statement

➢Use the else if statement to specify a new condition if the first condition is false.

EXAMPLE:

if (hour < 10) {
    greeting = "Good morning";
} else if (hour < 20) {
    greeting = "Good day";
} else {
    greeting = "Good evening";
}

Prev Page Next Page