Conditional Operator

JScript supports an implicit conditional form, the conditional operator. It takes three operands. A question mark separates the first two operands, and a colon separates the second and third operands. The first operand is a conditional expression. The second operand is a statement that is executed if the conditional expression evaluates to true. The third operand is executed if the conditional is false. For more information, see Conditional (Ternary) Operator (?:). The conditional operator is similar to the if...else statement.

Using the Conditional Operator

In this example, the conditional operator determines if an hour in 24-hour time is before noon ("AM") or after noon ("PM").

var hours : String = (the24Hour >= 12) ? " PM" : " AM";

In general, an if ... then ... else structure is appropriate when choosing between statements to be executed, whereas the conditional operator (?:) is appropriate when choosing between two expressions. Do not try to use the conditional operator to choose between more than two alternatives or to execute blocks of statements. In those cases, use the if...then...else construct.

See Also

Other Resources

JScript Conditional Structures

JScript Reference