Conditional (Ternary) Operator ‘?’

Conditional (Ternary) operator takes in three operands: a condition, question mark and an expression to execute if condition is truthy and an expression if condition is falsy.

Syntax of the ternary operator:

Condition is evaluated and if truthy, value1 is return otherwise value2.

Example below demonstrate the use of ternary operator.

This let us write code in simple and shorter way. It could have been longer had it been the case of using ‘if..else’ statements.