React Logical AND Rendering

In the last lesson you learned the React Ternary Operator for picking between two things. This lesson is about the cleaner tool for when you just want to show one thing or nothing: the && operator.

🤔 Why do we need && rendering?

The ternary is great for an A or B choice. But sometimes there is no B.

  • A lot of the time you want to show something OR show nothing. There’s no “else” case.
  • Think of a notification dot on an app icon. It shows when there’s something new, and it just disappears when there isn’t.
  • Writing condition ? <Badge /> : null works, but that : null is noise you have to type every time.
  • For “show this when true, else show nothing”, React has a shorter pattern. It’s the && operator.

So the short version: use the ternary for “this or that”, and && for “this or nothing”.

🧩 How does && work?

The && operator is plain JavaScript, and React leans on a small trick in how it behaves.

  • && looks at the value on its left first.
  • If the left side is truthy (basically “true-like”), it gives back the value on the right.
  • If the left side is falsy (“false-like”), it stops right there and gives back the left value instead. It never even looks at the right side.
  • This stopping early is called short-circuiting. That’s the whole magic React uses here.

Here is the idea in plain JavaScript, before we bring React into it.

true && "show me"; // → "show me" (left is true, so we get the right side)
false && "show me"; // → false (left is false, so it stops and returns false)

So && is like a gatekeeper. If the left passes, you get the right side through. If not, you get the left side back and the right side is skipped.

💡 Using && in JSX

Now put that gatekeeper inside JSX. React renders the right side only when the left condition is true.

  • You write {condition && <Element />} inside your JSX.
  • If condition is true, React renders <Element />.
  • If condition is false, the expression returns false, and React simply renders nothing for it.
  • React quietly ignores false, null, and undefined. So a false left side means an empty screen, exactly what you want.

Here is a message badge that only appears when there is at least one unread message.

import { useState } from "react";
function Inbox() {
const [unreadCount, setUnreadCount] = useState(3);
return (
<div>
<h1>Inbox</h1>
{unreadCount > 0 && <span>You have new messages</span>}
</div>
);
}

So when unreadCount is 3, the condition unreadCount > 0 is true, so React shows the badge. If it drops to 0, the condition becomes false and the badge just disappears. No : null needed.

When to reach for &&

Use && when you want to show one thing or nothing. If you actually need to choose between two different things, use the ternary instead. && has no “else” side.

🔔 A real example: a notification dot

Let’s do something you’ve seen a hundred times: a little red dot on a bell icon that only shows when there’s something new.

  • We keep a count of new notifications in state.
  • The bell icon is always there on screen.
  • The red dot uses &&, so it only appears when the count is above zero.
  • Mark everything as read, the count goes to zero, and the dot quietly disappears.

Here is the bell with its conditional dot.

import { useState } from "react";
function NotificationBell() {
const [newCount, setNewCount] = useState(2);
return (
<button onClick={() => setNewCount(0)}>
🔔 Notifications
{newCount > 0 && <span> ({newCount} new)</span>}
</button>
);
}

So while newCount is 2, the button shows ”🔔 Notifications (2 new)”. Click it, the count goes to 0, the condition turns false, and the “(2 new)” part vanishes. The bell stays, only the extra bit comes and goes.

⚠️ The classic pitfall: a 0 shows up on screen

Here is the trap that catches almost everyone the first time. You use a number directly on the left of &&, and a stray 0 appears on your page.

  • Say you write {items.length && <List />} to show a list only when there are items.
  • When the list is empty, items.length is 0.
  • Remember how && works? If the left is falsy, it returns the left value. And 0 is falsy.
  • But here’s the catch. React does NOT ignore the number 0 like it ignores false. It actually renders 0 on the screen.

So you get a lonely 0 sitting on your page where you expected nothing. Here’s the wrong way versus the right way.

// ❌ Wrong: items.length is 0 when empty, and React renders that 0 on screen
{items.length && <List items={items} />}
// ✅ Right: a real true/false, so a false left side renders nothing
{items.length > 0 && <List items={items} />}

The fix is to make the left side a real boolean, not a number. items.length > 0 is either true or false, never 0. So when it’s false, React renders nothing, just like you wanted.

Numbers on the left of && are dangerous

React ignores false, null, and undefined, but it DOES render 0. So a number on the left of && can leak a 0 onto the screen. Always make the left side a real boolean.

🛠️ Two clean ways to fix the 0 bug

You don’t have to memorize anything tricky here. There are two easy ways to turn a number into a real boolean.

  • Compare it: count > 0. This is the clearest and reads like plain English.
  • Force it to a boolean with !!: !!count. The double !! flips the value to a boolean true or false.
  • Both turn 0 into false, so React renders nothing instead of a stray 0.
  • Prefer the comparison when you can. count > 0 tells the next reader exactly what you mean.

Here are both fixes side by side so you can see they do the same job.

// Both render nothing when count is 0, no stray 0 on screen
{count > 0 && <Badge count={count} />} // compare: clearest
{!!count && <Badge count={count} />} // !! forces a real boolean

So either one kills the 0 bug. The > 0 version is usually nicer to read, but the !! trick is handy when you just want a quick truthy check on any value.

What the user sees when count is 0

With {count && ...} → 0 (the bug: a stray zero appears)
With {count > 0 && ...} → (nothing, which is correct)
With {!!count && ...} → (nothing, which is correct)

🧩 What You’ve Learned

  • {condition && <Element />} shows the element when the condition is true, and nothing when it’s false
  • && short-circuits: if the left side is falsy, it returns the left value and skips the right side
  • ✅ React ignores false, null, and undefined, so a false left side renders nothing on screen
  • ✅ Use && for “show this or nothing”, and the ternary when you need “this or that”
  • ✅ A number like 0 on the left is the classic bug, because React actually renders the 0
  • ✅ Fix it by making the left a real boolean, with count > 0 or !!count

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does {isLoggedIn && <Profile />} render when isLoggedIn is false?

    Why: When the left side is false, the && expression returns false. React ignores false, null, and undefined, so nothing shows up on the screen.

  2. 2

    Why does {items.length && <List />} sometimes show a 0 on the screen?

    Why: When the array is empty, items.length is 0. The && returns that 0 because it is falsy. Unlike false or null, React actually renders the number 0 on the screen.

  3. 3

    What is the correct fix for the stray 0 bug?

    Why: Making the left side a real boolean fixes it. items.length > 0 is true or false, never 0, so a false left side renders nothing instead of a stray 0.

  4. 4

    When should you use && instead of the ternary operator?

    Why: Use && for 'show this or nothing' because it has no else side. When you need to pick between two different things, the ternary is the right tool.

🚀 What’s Next?

Now you can show or hide a single element cleanly, and you know how to dodge that sneaky 0 bug. Next you’ll use these same ideas to show a loading message while your data is on its way, then swap it for the real content once it arrives.

React Rendering Loading States

Share & Connect