Conditional Operator

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

Syntax

condition ? expr1 : expr2

Parameters

condition 
An expression that evaluates to 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:

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

Tags (1)

Edit tags

Attachments (0)

 

Attach file