Conditionally execute (?:) (Operator/conditional)

Conditionally execute one code branch or another.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Property/method value type:Depends on arguments
JavaScript syntax:-aCondition ? someCode : moreCode
Argument list:aConditionA relational or logical expression that yields true or false
moreCodeCode that is executed if aCondition is false
someCodeCode that is executed if aCondition is true

The two associated code blocks are executed according to the value yielded by a Boolean test on the first operand. If it is true, then the first code block is executed, otherwise the second is used.

The associativity is right to left.

Refer to the Operator Precedence topic for details of execution order.

This is sometimes called a Ternary operator, because it takes three operands.

Example code:

   <HTML>

   <HEAD></HEAD>

   <BODY>

   <SCRIPT>

   mySwitch = false;

   myResult = (mySwitch) ? "TRUE VALUE" : "FALSE VALUE" ;

   document.write(myResult);

   </SCRIPT>

   </BODY>

   </HTML>

See also:Associativity, Conditional expression, Flow control, if( ... ) ..., if( ... ) ... else ..., Operator Precedence, Selection statement, Ternary operator

Cross-references:

ECMA 262 edition 2 - section - 11.12

ECMA 262 edition 3 - section - 11.12