Exercise

Conditional statements in JavaScript are used to make decisions in code based on specified conditions.

In JavaScript, there are mainly two types of conditional statements: if statement and switch statement.

You can use the if statement in JavaScript to execute a block of code if a specified condition is true.

The syntax of the if statement in JavaScript is as follows:

javascript

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

The else statement in JavaScript is used in conjunction with the if statement to execute a block of code if the condition is false.

You can use the else statement in JavaScript to execute a block of code if the condition specified in the if statement is false.

javascript

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

The else if statement in JavaScript allows you to specify multiple conditions to execute different blocks of code.

You can use the else if statement in JavaScript to specify additional conditions to be tested if the previous condition(s) are false.

javascript

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 all conditions are false
}

The switch statement in JavaScript is used to perform different actions based on different conditions.

You can use the switch statement in JavaScript to evaluate an expression and execute a corresponding case block based on the value of the expression.

javascript

switch (expression) {
    case value1:
        // code to be executed if expression matches value1
        break;
    case value2:
        // code to be executed if expression matches value2
        break;
    default:
        // code to be executed if expression doesn't match any case
}