If Statement, Else Clause
Working with data using JavaScript includes making decisions about what to do with that data. One example of this is determining if a form should be submitted based on whether or not the end user has properly filled out the form. As you can see, testing data using conditional statements is vital to writing meaningful scripts.
Conditional Decision Making
One of JavaScript's implementations of conditional decision making is the simple
if statement with the optional
else clause. The if statement provides a way to execute one set of instructions only if a stated condition is true. If the stated condition evaluates to false, the set of instructions will be skipped. Optionally, an else clause may execute another set of instructions if the condition is false. In all cases, any code following the if/else statement's body will be executed.
If Statement Dissection
There are three parts to an if statement: the if keyword, the conditional expression contained within parentheses and executable code (referred to as a command block) contained within an opening and closing curly brace. The structure of an if statement is as follows:
if (conditional expression evaluates to true) {
then execute this code
}
The Optional Else Clause
If you need to execute an alternate set of statements if the condition evaluates to false, you can include an else clause. Think of it like this: if this is true, do this. if this is not true, do this instead.
if (conditional expression evaluates to true) {
execute this code
}
else {
execute this code
}
When there are only two possible conditions to test for, an if statement with an else clause is all you need. When there are many possible conditions to test for, multiple if statements may be required. An if statement's command block may even contain another if statement.
Testing For Multiple Conditions
Each else clause belongs to the nearest preceding if statement. Because of this, it is not possible to have one catch all else clause act as a net for multiple if statements just because it follows a series of them. One final else clause can, however, act as a catch all net as long as each if statement, beginning with the second, is preceded by an else. The final else will then perform as a catch all because all the if statements are now attached to each other by the else statements.
if (name == "mini") {
alert("your name is mini")
}
else if (name == "harold") {
alert("your name is harold")
}
else if (name == "fred") {
alert("your name is fred")
}
else {
alert("Your name is not mini, harold or fred")
}
Gotcha Warning
Notice the double equals signs in each of the above conditional expressions. It is common to mistakingly use the assignment operator (=) rather than the comparison operator (==) when testing a value in a conditional expression. When a script is not performing as expected, look to see if your if statement contains this error.
>>
Functions
>>
JavaScript Variables
>>
JavaScript Arguments
>>
JavaScript Operators, Expressions and Evaluation
>>
Special JavaScript Operators
>>Decision Making: If Statement, Else Clause