?: (Conditional operator)

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

Syntax

condition ? expr1 : expr2

Parameters

Condition—An expression that evaluates to either true or false.

expr1, expr2—Expressions with values of any type.

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:

Console.Write ("The fee is " + (isMember ? "$2.00" : "$10.00"))