Search⌘ K
AI Features

Conditional Statement

Explore the use of conditional statements in JavaScript to control program flow based on different conditions. Learn to write if else structures, use comparison and logical operators, and handle multiple branches with practical coding examples like eligibility checks and pay calculations.

The most commonly used conditional statement in JavaScript is the if statement. It contains a conditional expression and a true branch, and it may contain a false branch. The true branch is executed when the conditional expression is true. The false branch, if present, is executed when the conditional expression is false. The following program demonstrates the use of the conditional statement.

Note: The true branch of the if statement starts the execution after the starting brace { and executes all the statements until the ending brace }.

Syntax

if (conditional statement) { // no semicolon here
    // execution starts from here if the conditional statement is true

    // until here
} else { // no semicolon here
    // if the if branch is not executed then this part of the code is executed

    // until here
}

We want to write a program that determines eligibility for a driver’s license based on the age that has been input by the user. The eligible age should be 1818 or above.

The conditional statement used in the program above starts with an if followed by a conditional expression.

Note: ...