JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Operators

javascript
junior
operators

What is the ternary operator and how does it differ from an if-else statement?

ternary
operators
conditional
jsx
fundamentals
Quick Answer

The ternary operator (condition ? valueIfTrue : valueIfFalse) is a concise one-line alternative to if-else that returns a value. Unlike if-else, it's an expression that can be used in assignments, return statements, and JSX.

Detailed Explanation

The ternary (conditional) operator is the only JavaScript operator that takes three operands.

Syntax: condition ? expressionIfTrue : expressionIfFalse

Key differences from if-else:

  • Expression vs Statement: Ternary is an expression (produces a value), if-else is a statement
  • Assignment: Ternary can be used directly in variable assignments
  • Return values: Can be used inline in return statements and template literals
  • JSX: Essential in React for conditional rendering (if-else can't be used inside JSX)

When to use ternary:

  • Simple conditional assignments
  • Inline conditional rendering (React/JSX)
  • Short, readable one-liners

When to use if-else:

  • Complex logic with multiple statements
  • Side effects (API calls, DOM updates)
  • More than 2 branches (avoid nested ternaries)

Code Examples

Ternary vs if-elseJavaScript
// if-else statement
let message;
if (age >= 18) {
  message = 'Adult';
} else {
  message = 'Minor';
}

// Ternary expression — same result, one line
const message = age >= 18 ? 'Adult' : 'Minor';

// In template literals
console.log(`Status: ${isActive ? 'Active' : 'Inactive'}`);

// In function returns
function getDiscount(isMember) {
  return isMember ? 0.2 : 0;
}

// In React JSX
const Badge = ({ status }) => (
  <span className={status === 'online' ? 'green' : 'gray'}>
    {status === 'online' ? 'Online' : 'Offline'}
  </span>
);

Real-World Applications

Use Cases

Conditional Class Names

Applying CSS classes based on component state, e.g., isActive ? 'btn-active' : 'btn-inactive'

React Conditional Rendering

Rendering different components or elements based on props or state directly inside JSX

Inline Fallback Values

Providing inline default values like const label = value ? value : 'N/A' for display purposes

Mini Projects

Conditional UI Builder

beginner

Build a component that toggles between different UI states using ternary expressions

Refactoring Exercise

intermediate

Practice converting if-else chains to ternaries and vice versa, evaluating readability trade-offs

Industry Examples

clsx

Utility for conditionally constructing className strings, replacing ternary-heavy class logic in React components

Prettier

The experimentalTernaries option (v3.1+) reformats ternary expressions using a curious ternary style for improved readability

Resources

MDN - Conditional (ternary) operator

docs

JavaScript.info - Conditional branching

article

Related Questions

What is the difference between == and === in JavaScript?

junior
operators
Previous
How does type coercion work in JavaScript?
Next
What is a Single Page Application (SPA) and how does it work?
PrevNext