Conditional rendering
Rendering different JSX based on a condition with &&, ternaries, and early returns — and the classic 0 && pitfall that renders a literal zero.
Conditional rendering
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- JSX's {} slots only accept expressions, not statements, so conditional rendering is written with expression-based tools — &&, the ternary operator, or by choosing what to return before the JSX even starts.
- a && <Thing /> renders <Thing /> when a is truthy; when a is falsy, it renders whatever a itself evaluated to, which is usually what you want — unless that falsy value is 0.
- 0 && <Thing /> renders the literal number 0 on the page, because 0 is falsy and short-circuits the expression to 0 itself, and React happily renders a number. The fix is forcing a boolean first, e.g. count > 0 && <Thing />.
- A ternary (condition ? a : b) is the right tool when you need to choose between two different things to render, not just show-or-hide one.
- An early return inside a component — returning one piece of JSX (or null) before the rest of the function runs — is often the clearest way to handle a condition that excludes everything else, like a loading or error state.
- Returning null from a component is valid and renders nothing at all; it's the standard way to say this component has nothing to show right now.
Expressions only, so statements won’t work
JSX’s {} slots accept expressions, not statements — covered in the JSX lesson, and it’s the reason conditional rendering in React looks different from conditional logic in plain imperative code. You simply cannot write this:
// This is a syntax error — if is a statement, not an expression.
function Badge({ count }) {
return (
<span>
{if (count > 0) { <strong>{count}</strong> }}
</span>
);
}
Every pattern below exists to work around that one constraint: choosing what to render using tools that produce a value.
&& for show-or-hide
The most common pattern for “render this, or render nothing” is the logical AND operator:
function Badge({ count }) {
return (
<span>
{count > 0 && <strong>{count}</strong>}
</span>
);
}
&& evaluates its left operand first. If it’s truthy, the expression evaluates to (and JSX renders) the right operand. If it’s falsy, the expression short-circuits and evaluates to whatever that falsy left operand actually was. React treats false, null, and undefined specially — it renders none of them, i.e. nothing appears. That’s exactly what you want here: count > 0 evaluates to a boolean, so when it’s false, nothing renders.
The 0 && pitfall
Here’s the trap: && doesn’t convert its left side to a boolean before deciding what to render — it evaluates to the left side’s actual value when that value is falsy. Watch what happens if you skip the comparison and hand && the number itself:
function Badge({ count }) {
return (
<span>
{count && <strong>{count}</strong>}
</span>
);
}
When count is 0, this is 0 && <strong>0</strong>. 0 is falsy, so the expression short-circuits — not to false, but to 0 itself, because that’s the actual value && stops on. React renders that 0 as visible text on the page. The bug is subtle precisely because it’s easy to write and easy to miss in review: the component looks correct for every count except exactly zero, where a stray “0” appears on screen that nobody intended.
The fix is one character of discipline: always make sure the left side of a rendering && is already a real boolean, not a number (or any other value) that happens to be falsy.
// Fixed — count > 0 is a boolean, so the falsy branch is `false`, not `0`.
{count > 0 && <strong>{count}</strong>}
This is worth internalizing as a habit, not just a one-off fix: any time you reach for && to conditionally render, check what your left operand actually evaluates to when it’s falsy. If it could be 0, an empty string "" behaves the same way as a string but is at least invisible, while NaN has the identical problem to 0.
Ternaries for choosing between two things
&& only has a “render this, or render nothing” shape. When you need to render one of two different things, the ternary operator is the right tool:
function Status({ isLoading, data }) {
return (
<div>
{isLoading ? <Spinner /> : <DataView data={data} />}
</div>
);
}
condition ? a : b evaluates to a when condition is truthy and b otherwise — both branches are explicit, so there’s no falsy-value ambiguity the way there is with &&.
Early returns for “this excludes everything else”
When a condition determines the component’s entire output — not just one piece of it — an early return is often clearer than nesting everything else inside a ternary:
function UserProfile({ user, isLoading, error }) {
if (isLoading) {
return <Spinner />;
}
if (error) {
return <p role="alert">Something went wrong: {error.message}</p>;
}
if (!user) {
return null;
}
return (
<div>
<h2>{user.name}</h2>
<p>{user.email}</p>
</div>
);
}
Each if handles a case that fully determines what renders, before the function ever reaches the “normal” JSX at the bottom. This keeps the main return statement focused on the happy path, instead of burying it inside nested ternaries that get hard to read past two or three branches.
Returning null
Notice the if (!user) return null; line above. Returning null from a component is completely valid in React, and it means “render nothing here” — no DOM node appears at all, not even an empty one. This is the standard way to express “this component legitimately has nothing to show right now,” as opposed to an error state or a loading state, which do have something to show.
What the video covers
Codevolution’s episode, current as of their React 19 series, covers all four of this lesson’s core patterns — &&, ternaries, early returns, and returning null — in one focused walkthrough. Whether it explicitly calls out the 0 && literal-zero pitfall by name is worth less attention than this lesson’s own section above either way, since that pitfall is exactly the kind of gotcha that’s easy for a fast-paced tutorial to gloss past; the ByteGrad video linked below gives it dedicated, focused treatment if you want a second pass on just that one issue.
Where this fits
Conditional rendering decides whether something appears at all; the next lesson covers rendering a dynamic number of things — rendering lists and keys.
Check yourself
Why can't you write an if statement directly inside a JSX {} expression slot?
Because {} in JSX only accepts an expression — something that evaluates to a value — and an if statement doesn't produce a value, it's a control-flow statement. Conditional rendering instead relies on expression-based constructs: &&, the ternary operator, or deciding what to return before you get to the JSX.
What renders on the page when you write {0 && <Badge />}, and why?
The literal number 0 renders on the page, as visible text. && evaluates its left side first; since 0 is falsy, the whole expression short-circuits and evaluates to 0 itself (not to false), and React renders that value as text. React skips rendering only for true falsy non-values like false, null, and undefined — not for 0.
How do you fix the 0 && <Badge /> pitfall so nothing renders when the count is zero?
Force the left side to an actual boolean before the &&, for example count > 0 && <Badge />. Since count > 0 evaluates to true or false rather than to the number itself, the expression short-circuits to false when the count is zero, and React renders nothing for a false value.
When should you reach for a ternary instead of && for conditional rendering?
Use && when you want to render something or render nothing. Use a ternary (condition ? a : b) when you need to choose between two different things to actually show — for instance, a loading spinner versus the loaded content — since && alone has no way to express an alternative branch.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.