What is a ternary operator?

The ternary operator tests a condition by comparing two values and returns a third value based on the results. A ternary operator can often simplify a traditional if-else statement.

Syntax

Condition ? First_Result : Second_Result

Example: Ternary operator

Let’s have an example of a ternary operator.

Get hands-on with 1400+ tech skills courses.