The conditional operator allows a conditional assignment to a variable, depending on the Boolean result of the evaluation.

The following example explains how it works:

var value = (expression) ? operand1 : operand2

The Boolean expression from above (operand1 == operand2) is evaluated first. If the result is true (if operand1 is equal to operand2), the operand1 will be assigned to the variable value; in the opposite case (if they are not equal), the operand2 will be assigned to it.

This way of conditional evaluation is a very widely used technique in JavaScript.

Example

The example with conditional operators in JavaScript:

 

›› go to examples ››