QamoosTech
Programming FundamentalsLanguage basicsIntermediatePythonJavaScript

Ternary Operator

PronunciationTER-nuh-ree OP-er-ay-ter

Definition

The ternary operator is a one-line conditional that picks between two values depending on a condition.

Where you hear it

In JavaScript and Python code reviews, when someone shortens a small if / else into one expression.

Examples

  • In Python it reads label = "adult" if age >= 18 else "minor".
  • Use a ternary for a simple choice, but not for nested logic.
  • The status label is a ternary: paid if the invoice is settled, otherwise due.

Common mistake

Nesting ternaries inside each other. It becomes unreadable fast; use a normal if instead.

Don't confuse with

A full if / else statement, which runs blocks of code. A ternary is an expression that produces a value.

Say it at work

  • In a meeting or chatA ternary is fine here, but keep it on one line.
  • In an email or ticketThat nested ternary is hard to read; please expand it.