The if Statement
else/if Clause
else Clause



Navigation Aids -- This Page           Navigation Aids -- This Topic           Navigation Aids -- This Site




Review of Terms Related to This Topic





Definition and Purpose

The if statement is one of JavaScript's program control statements. This statement and it's variations will give the programmer the ability to control program flow with the decision making qualities inherent in the statement. The if statement contains an expression that evaluates to a boolean. Subsequent statements will execute conditionally based on the boolean value. The optional else clause is executed upon the false condition.





Statement Syntax


if (expression)    statements else    statements



More on Syntax and Statement Behavior





if Statements Evaluate to Boolean

The conditional expression of the if statement will evaluate to a boolean. The statements immediately following the conditional expression are executed when the expression is true. Otherwise, the program flow precedes to the next sequential statements immediately after the if statement (unless the else keyword is included). When the else clause is included, then it's associated statements are executed when the conditional expression evaluates to false. Of course the else statements are skipped when the conditional expression evaluates to true.





if Statements and Block Statements

The conditional statements are grouped with curly braces. This is a use of the statement block. The if statement is just one example of where the block statement may be used.





The else/if Clause

More advanced logic can be added using the else/if clause. Simply add another if statement to the else clause. if (conditional expression1)   a statement or block else if (conditional expression2)   a statement or block else if (conditional expression3)   a statement or block ..... else   a statement or block

The last else must be a stand alone clause. The whole else/if structure can be better represented with the switch statement. The switch statement is JavaScripts version of the traditional case clause.





Nested if Statements

You can nest if statements within one another. This structure allows you to drill down to a specific detail. if (conditional expression1)   if (conditional expression2)     if (conditional expression3)       a statement or block     else       a statement or block   else     a statement or block else   a statement or block

Each else clause is paired with the last unpaired if clause.





ECMA-262 Section 12.5

The if statement is an ECMAScript core statement and is detailed is section 12.5 of the ECMAScript Language Specification. Here is a partial extraction of text from this section.

Each else for which the choice of associated if is ambiguous shall be associated with the nearest possible if that would otherwise have no corresponding else.





Some Script Examples


Feature Script
Complex expression with logical OR: the conditional expression evaluates to true when either expression is true if ((name == "") | | (name == null))
Complex expression with logical AND: the conditional expression evaluates to true when both expressions are true if ((brand == "HP") && (type == "printer"))


Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson