Operator Kondisional ECMAScript

Conditional Operator

The conditional operator is the most functional operator in ECMAScript, and its form is the same as in Java.

variable = boolean_expression ? true_value : false_value;

This expression is mainly based on boolean_expression The conditional assignment of the calculation result of the variable is conditional. If Boolean_expression If it is true, then true_value Assign to variable; if it is false, then false_value Assign to variable.

For example:

var iMax = (iNum1 > iNum2) ? iNum1 : iNum2;

In this example, iMax will be assigned the maximum value in the number. The expression statement assigns iNum1 to iMax if iNum1 is greater than iNum2. But if the expression is false (i.e., iNum2 is greater than or equal to iNum1), then iNum2 is assigned to iMax.