Availability: |
| ||||||||
JavaScript syntax: | - | aLabel: if(aCondition) { someCode} else { someOtherCode ) | |||||||
Argument list: | aCondition | An expression that yields a Boolean value | |||||||
aLabel | An optional identifier to name the if block | ||||||||
someCode | Some script source text that is executed if the condition tests true | ||||||||
someOtherCode | Some script source text that is executed if the condition tests false |
The if() ... else statement is used to conditionally execute one or other code block depending on the result of a conditional expression.
The expression in the parentheses is evaluated and cast to a Boolean value. If it yields a true value as a result, then the code in the first associated block is executed. Otherwise, the code in the first block is ignored and the code in the block following the else keyword is executed.
Each else keyword for which the associated if() is ambiguous will be associated with the nearest possible if() that would otherwise have no corresponding else.
At version 1.2 of JavaScript, you can name the if statement and use the labelled form of the break keyword to exit the conditional code block prematurely.
// recommended form if(a == b) { z = 100; } else { z = 200; } // Possibly dangerous during maintenance if(a == b) z = 100; else z = 200;
ECMA 262 edition 2 - section - 12.5
ECMA 262 edition 3 - section - 12.5
Wrox Instant JavaScript - page - 22
Prev | Home | Next |
if( ... ) ... | Up | IFRAME object |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |