Short-Circuit Evaluation in React: Cleaner Code with a Catch


If you've spent any time building React components, chances are you've needed to conditionally render elements. One pattern that often pops up is using the logical &&
operator to decide when something should be shown on the screen.
It looks clean. It feels efficient. But it can also quietly introduce bugs into your UI. Let’s explore how it works, why it's useful, and when it might betray you.
🧩 Conditional Rendering: A Quick Setup
Imagine this simple React setup:
import React, { useState } from 'react';
import Welcome from '../components/Welcome';
function HomePage() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
return (
<div>
{isLoggedIn && <Welcome />}
</div>
);
}
export default HomePage;
In the example above, the <Welcome />
component only appears when isLoggedIn
is true
. Otherwise, nothing is rendered at all. This is thanks to short-circuit evaluation.
🧠 What Is Short-Circuit Evaluation?
In JavaScript, the &&
operator evaluates the left side first. If it’s truthy, the right side is evaluated and returned.
true && 'Hello'; // 'Hello'
false && 'Hello'; // false
In JSX, that right-side value could be a component:
{showAlert && <Alert message="Something went wrong" />}
When showAlert
is true
, the component is rendered. If it’s false
, React skips it—nice and clean.
⚠️ The Subtle Danger: Falsy but Rendered
What if the variable isn't strictly true
or false
?
Take this example:
{notifications && <p>You have {notifications} notifications</p>}
Now say notifications = 0
. In JavaScript, 0
is falsy, so the component won’t render— but React might still show the value depending on how things are structured.
This could cause confusing UI behavior like displaying a lone 0
in unexpected places.
✅ Safe Alternatives That Work Better
1. Use a Boolean Expression
{notifications > 0 && <p>You have {notifications} notifications</p>}
2. Force Boolean with Double Bang !!
{!!notifications && <p>You have {notifications} notifications</p>}
3. Ternary Operator (Clear Intent)
{notifications > 0 ? <p>You have {notifications} notifications</p> : null}
4. Rendering a List Safely
{list.length > 0 && list.map(item => <li key={item}>{item}</li>)}
These methods eliminate the risk of accidentally rendering 0
, undefined
, or null
.
🔄 What Should You Avoid?
Avoid using &&
when the left-hand value might be:
0
""
(empty string)null
undefined
Even though these are falsy, React may still print their values, leading to unexpected results.
🎯 When &&
Is a Good Fit
✅ When the condition is strictly true
or false
:
{isLoading && <Spinner />}
{isAdmin && <AdminPanel />}
Avoid when the value can be anything else.
🙏 Thank You!
Thank you for reading!
I hope you enjoyed this post. If you did, please share it with your network and stay tuned for more insights on software development. I'd love to connect with you on LinkedIn or have you follow my journey on HashNode for regular updates.
Happy Coding!
Mitesh Kukdeja
Subscribe to my newsletter
Read articles from Mitesh Kukdeja directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mitesh Kukdeja
Mitesh Kukdeja
Turning ideas into smooth, scalable mobile experiences — one line of code at a time. Hi, I’m Mitesh Kukdeja — a passionate React Native developer with 2+ years of hands-on experience building cross-platform apps that delight users and deliver results. From health and fitness platforms to job boards and music contest apps, I’ve helped bring a wide range of product visions to life. What sets me apart is my obsession with clean, reusable code and user-centric UI/UX. I specialize in React Native, TypeScript, Redux Toolkit, Firebase, and REST API integration—making sure every app I build is responsive, secure, and ready for scale. I’ve also deployed apps to both the Play Store and App Store, managing the full release cycle. My projects have included integrating real-time features like video conferencing (Agora), personalized push notifications, and advanced security implementations for enterprise clients like Godrej. Whether it’s debugging a performance bottleneck or designing a scalable component architecture, I’m all in. My goal is to keep solving meaningful problems through technology while collaborating with creative minds. I thrive in fast-paced environments where innovation and impact matter. If you’re building something exciting in mobile or looking for a tech partner who values quality and performance — let’s connect!