LearnContact
Lesson 1722 min read

Conditional Rendering

Learn how to show different UI based on conditions in React using if statements, the ternary operator, the && operator, and returning null.

Introduction

Real interfaces rarely show the exact same thing every time — a loading spinner while data fetches, an error message if something fails, a "Log In" button instead of a "Log Out" button. Deciding what to render based on the current state is called conditional rendering, and React does not need a special syntax for it: it just uses plain JavaScript.

In this lesson you will learn the four main patterns for conditional rendering: a plain if statement before your return, the ternary operator for inline either/or choices, the && operator for showing something only when a condition is true, and returning null when you want to render nothing at all.

What You Will Learn
  • How to use a regular if statement before a component's return.
  • How to use the ternary operator (condition ? a : b) inline in JSX.
  • How to use && to render something only when a condition is true.
  • The classic bug where a literal 0 gets rendered unexpectedly.
  • How to render nothing at all by returning null.

Using an if Statement

The simplest way to conditionally render something in React is to use a regular JavaScript if statement before your component's return, deciding what JSX to produce.

Greeting.jsx
function Greeting({ isLoggedIn }) {
  if (isLoggedIn) {
    return <h1>Welcome back!</h1>;
  }

  return <h1>Please log in.</h1>;
}
Rendered Output
<Greeting isLoggedIn={true} />  ->  Welcome back!
<Greeting isLoggedIn={false} /> ->  Please log in.

This approach works great when the two outcomes are very different pieces of UI, or when you have more than two branches to handle.

When to Reach for an if Statement

Use a plain if (with early returns) when a component has multiple, clearly distinct states to handle — for example returning a loading view, an error view, and a success view from the same component.

The Ternary Operator

When you need to choose between two small pieces of JSX inline, without a full if statement, the ternary operator (condition ? valueIfTrue : valueIfFalse) is the standard tool. It works directly inside curly braces in your JSX.

StatusBadge.jsx
function StatusBadge({ isOnline }) {
  return (
    <p>
      Status: {isOnline ? "🟢 Online" : "🔴 Offline"}
    </p>
  );
}
Rendered Output
<StatusBadge isOnline={true} />  ->  Status: 🟢 Online
<StatusBadge isOnline={false} /> ->  Status: 🔴 Offline

The ternary shines for small, either/or decisions embedded right inside the markup, keeping the two possible outcomes visually close together.

Compact

Fits directly inline inside JSX curly braces, with no separate variable needed.

Either/Or

Perfect for exactly two outcomes, like showing one label or another.

Readability Limit

Nesting multiple ternaries inside each other quickly becomes hard to read.

The && Operator

Sometimes you only want to render something when a condition is true, and render nothing otherwise — there is no "else" case. For this, React code commonly uses the && operator, relying on how JavaScript short-circuits: if the left side is falsy, JavaScript never evaluates the right side, and nothing is rendered.

Inbox.jsx
function Inbox({ unreadCount }) {
  return (
    <div>
      <h2>Inbox</h2>
      {unreadCount > 0 && <p>You have {unreadCount} unread messages.</p>}
    </div>
  );
}
Rendered Output
<Inbox unreadCount={3} /> ->
Inbox
You have 3 unread messages.

<Inbox unreadCount={0} /> ->
Inbox
How the Short-Circuit Works

unreadCount > 0 evaluates to true or false first. If it is false, JavaScript stops right there and the whole expression evaluates to false, which React renders as nothing at all.

The Classic Zero Bug

The && pattern has one famous pitfall. If the left-hand value is the number 0 (not a boolean), JavaScript's short-circuit still stops there — but the expression evaluates to 0, not false. Unlike false, null, or undefined, React does render the number 0 onto the screen.

CartBuggy.jsx
function Cart({ itemCount }) {
  return (
    <div>
      {/* Bug: renders a literal "0" when itemCount is 0 */}
      {itemCount && <p>You have {itemCount} items in your cart.</p>}
    </div>
  );
}
Rendered Output (the bug)
<Cart itemCount={0} /> ->
0

The fix is to make sure the left side of && always evaluates to an actual boolean, not a number, by writing an explicit comparison such as itemCount > 0.

CartFixed.jsx
function Cart({ itemCount }) {
  return (
    <div>
      {itemCount > 0 && <p>You have {itemCount} items in your cart.</p>}
    </div>
  );
}
Rendered Output (fixed)
<Cart itemCount={0} /> ->
(nothing rendered)
Watch Out For This

Whenever you write count && <SomeJsx />, ask yourself whether count could ever be 0. If so, compare it explicitly: count > 0 && <SomeJsx />.

Rendering Nothing with null

Occasionally an entire component should render nothing at all, based on a condition. A React component is allowed to return null, and React simply skips rendering any DOM for that component.

Banner.jsx
function Banner({ message }) {
  if (!message) {
    return null; // render nothing when there is no message
  }

  return <div className="banner">{message}</div>;
}
Rendered Output
<Banner message="Sale ends today!" /> -> Sale ends today! (inside a div)
<Banner message="" />                 -> (nothing rendered)
<Banner />                             -> (nothing rendered)
null vs an Empty String

Returning null renders no DOM node at all. Returning an empty string or an empty <div></div> still renders something, even if it looks empty — the distinction matters when you are debugging layout or checking for a component in the DOM.

Common Mistakes

Avoid These Mistakes
  • Writing count && <Component /> when count can be 0, which renders a stray "0" on the page.
  • Nesting several ternary operators inside JSX until the markup becomes hard to follow.
  • Forgetting that returning undefined (instead of null) from a component throws an error in React.
  • Using if/else where a small ternary or && would have been clearer and more compact.

Best Practices

  • Use an if statement (with early returns) for multiple distinct states like loading, error, and success.
  • Use a ternary for a simple either/or choice embedded directly in JSX.
  • Use && only for "render this or nothing," and always guard against falsy-but-not-boolean values like 0.
  • Extract complex conditional JSX into a small helper function or variable if it starts to feel cluttered.
  • Return null explicitly (not undefined) when a component intentionally renders nothing.

Frequently Asked Questions

Can I use if statements directly inside JSX?

No, JSX curly braces only accept expressions, not statements. That is why if statements go before the return, while ternaries and && (which are expressions) work directly inside JSX.

Which is better, ternary or &&?

Use && when there is only one outcome to show (or nothing), and use a ternary when there are two different outcomes to choose between.

Why does React render 0 but not false or null?

React intentionally skips rendering booleans, null, and undefined so you can write conditions like show && <X /> naturally, but 0, being a valid displayable value in other contexts, is rendered as-is.

Is returning null from a component considered a bad practice?

No, it is a completely normal and recommended pattern for a component that should sometimes render nothing, such as a conditional banner or modal.

Can a ternary render two different components instead of text?

Yes, both branches of a ternary can be full JSX elements or components, not just text, for example isAdmin ? <AdminPanel /> : <UserPanel />.

Key Takeaways

  • Conditional rendering in React is just plain JavaScript — no special syntax is required.
  • Use an if statement with early returns for multiple distinct UI states.
  • Use the ternary operator (a ? b : c) for a simple inline either/or choice.
  • Use && to render something only when a condition is true, but guard against a literal 0.
  • Return null from a component to intentionally render nothing.

Summary

Conditional rendering is one of the most common things you will do in any React component, and it is built entirely from ordinary JavaScript: if statements, ternaries, and the && operator. Knowing when to reach for each one — and watching out for the classic "0" bug — keeps your JSX clean and bug-free.

In this lesson, you learned how to branch your UI with an if statement, choose between two outcomes with a ternary, show something only when true with &&, and render nothing at all with null. Next, you will learn how to render lists of data using .map() and why the key prop matters.

Lesson 17 Completed
  • You can branch component output using if statements.
  • You can write inline ternaries for either/or rendering.
  • You know how to use && safely, avoiding the literal-0 bug.
  • You can return null to render nothing from a component.
Next Lesson →

Lists and Keys