Conditional special operatorS2C Home « Operators « Conditional special operator
Shorthand conditional true/false operator.
Description
The conditional operator takes three operands and is often used as a shortcut replacement for the if....else
statement. The operator consists of a
condition and two expressions
- The value of the first expression is returned if the condition evaluates to
true
. - The value of the second expression is returned if the condition evaluates to
false
.
Syntax
Signature | Description |
---|---|
condition ? expressionIfTrue : expressionIfFalse | The conditional operator. |
Parameters
Parameter | Description |
---|---|
condition | Expression that evaluates to true or false . |
expressionIfTrue | The expression value to use for true . |
expressionIfFalse | The expression value to use for false . |
Examples
Following is an example of the conditional operator.
// The conditional operator.
var aBoolean = 'true';
alert((aBoolean == 'true' ? 'Evalutes to true' : 'Evalutes to false'));
alert((aBoolean == 'false' ? 'Evalutes to true' : 'Evalutes to false'));}
Related Tutorials
JavaScript Advanced Tutorials - Lesson 1 - Advanced Conditional Statements