The JavaScript Diaries: Part 5

The if/else Statement

Another type of conditional statement is called an if/else statement. It adds another dimension to the if statement. This statement allows for more complex decisions to be made within the script. The format is:

if (condition) {
action to be taken if the condition is true
}
else {
action to be taken if the condition is false
}

The else reserved word allows a determined action to be taken if the first condition is false. In other words, one way or another this portion of the script will be executed. If the condition is true, then the first statement will be executed; if the condition is false, the second statement will execute. Execution will then return to the main portion of the script.

Building upon our previous script, let's change it to an if/else statement.