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:

let variablename = condition ? value1 : value2; 

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

Example below demonstrate the use of ternary operator.

let qualifiedAge = (age > 20) ? 'qualified' : 'disqualified'; 

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.