Visit Mozilla.org

Core JavaScript 1.5 Reference:Operators:Special Operators:Conditional Operator

From MDC


Contents

[edit] Summary

The conditional operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

Operator
Implemented in: JavaScript 1.0
ECMA Version: ECMA-262

[edit] Syntax

condition ? expr1 : expr2

[edit] Parameters

condition 
An expression that evaluates to true or false.
expr1, expr2 
Expressions with values of any type.

[edit] Description

If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2. For example, to display a different message based on the value of the isMember variable, you could use this statement:

document.write ("The fee is " + (isMember ? "$2.00" : "$10.00"))