What is the ternary operator?
A one-line conditional expression that returns a value: condition ? a : b.
Expression — can be assigned or embedded, unlike if.
Keep it simple — avoid nesting ternaries.
Ideal for short value choices; use if/else for complex branching.
const age = 20;
const status = age >= 18 ? 'adult' : 'minor';
// In template literals
const msg = `You are ${age >= 18 ? 'an adult' : 'a minor'}`;
// Nested (avoid — hard to read)
const grade = score >= 90 ? 'A' : score >= 80 ? 'B' : score >= 70 ? 'C' : 'F';
// Prefer if/else or object lookup for complex conditions
const grades = { 90: 'A', 80: 'B', 70: 'C' };condition ? true : false replaces simple if/else. Clean for assignments and inline expressions.
Nested ternaries should use if/else or lookup tables instead.
Simple but know when NOT to use it: nested ternaries are unreadable. Object lookups for multiple conditions.
Most common use: const x = condition ? a : b.