Course contentsShow
← JavaScript
Lesson 229 of 2,7236. Booleans and ConditionalsFree lesson

When to Use Ternary vs If/Else

Recognize appropriate use cases: simple branches and inline assignments favor ternaries.

When to Use Ternary vs If/Else

You'll learn when ternary operators shine and when if/else statements are the better choice.

The Core Decision

Now that you know both ternary operators and if/else statements, how do you choose between them? The key is simplicity: ternaries work best for simple, inline decisions that produce a value, while if/else excels at complex logic with multiple statements.

When Ternaries Shine

Use a ternary when you need to:

  1. Assign a value based on a simple condition
const status = age >= 18 ? 'adult' : 'minor';
const price = isMember ? 10 : 15;
  1. Make quick inline decisions
console.log('You are ' + (score >= 50 ? 'passing' : 'failing'));

The ternary keeps your code compact and readable when the logic is straightforward.

When If/Else Is Better

Choose if/else when you have:

  1. Multiple statements to execute
  2. Complex logic that spans several lines
  3. Conditions that are hard to read in one line

Think of it like choosing between a quick yes/no question and a full conversation. If someone asks "Coffee or tea?" you can answer instantly (ternary). But if they ask "What would you like to drink, and should I add anything special?" you need a longer discussion (if/else).

The Readability Test

Ask yourself: "Can I read this condition in one breath without confusion?" If yes, ternary. If no, if/else.

Key Takeaway: Use ternaries for simple, single-value decisions and inline assignments; use if/else when you need multiple statements or complex branching logic. Readability always wins.