switch compares a value to cases (use break to avoid fall-through). Ternary cond ? a : b picks expressions inline.
switch tips
Use switch (true) pattern for ranges, or prefer object lookup maps for many string keys.
ternary
Great for simple assignments; nested ternaries harm readability—use if/else instead.
Important interview questions and answers
- Q: Missing break?
A: Falls through to next case—sometimes intentional, often a bug. - Q: Ternary vs if?
A: Ternary returns a value; if is a statement block.
Self-check
- What is fall-through?
- When avoid nested ternary?
Tip: Re-run the playground code for switch-ternary and tweak one line before the MCQs.
Interview prep
- Ternary use?
Simple value selection—not complex branching.