Availability: |
| ||||||||
Property/method value type: | Depends on arguments | ||||||||
JavaScript syntax: | - | aCondition ? someCode : moreCode | |||||||
Argument list: | aCondition | A relational or logical expression that yields true or false | |||||||
moreCode | Code that is executed if aCondition is false | ||||||||
someCode | Code 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.
<HTML> <HEAD></HEAD> <BODY> <SCRIPT> mySwitch = false; myResult = (mySwitch) ? "TRUE VALUE" : "FALSE VALUE" ; document.write(myResult); </SCRIPT> </BODY> </HTML>
Prev | Home | Next |
Conditional operator | Up | config.jsc |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |