Conditional Expressions (Ternary conditional)
Conditional expressions are like if-statements with the major difference and advantage of being in-line and allowing evaluation of specific expressions based on a condition.
If the CONDITION
evaluates to true
, the left side of the
:
operator is evaluated and returned, otherwise the right side is evaluated and
returned.
Syntax
Examples
Simple Conditional Expressions
Using Conditional Expressions to assign a value
Chained Conditional Expressions
As the ternary operator evaluates to more expressions, you can also chain it like this:
Equivalent to: