Javascript Conditional Operator

Introduction

The conditional operator is as follows:

variable = boolean_expression ? true_value : false_value; 

This allows a conditional assignment to a variable depending on the evaluation of the boolean_expression.

If it's true, then true_value is assigned to the variable.

If it's false, then false_value is assigned to the variable, as in this instance:

let max = (num1 > num2) ? num1 : num2; 

In this example, max is to be assigned the number with the highest value.

The expression states that if num1 is greater than num2, then num1 is assigned to max.

If the expression is false, then num2 is assigned to max.




PreviousNext

Related