Using If-Else and Ternary Operators in JSX
0 106
๐ Using If-Else and Ternary Operators in JSX
Conditional rendering is one of the most powerful techniques in React. It lets you decide what to show based on state, props, or other conditions. In JSX, you canโt use traditional if-else
directly inside the return โ but React gives us multiple ways to work around that! ๐ก
๐ Why We Need Conditional Rendering
Sometimes you want to:
- ๐ Show login/logout buttons based on authentication
- ๐ฆ Show loading spinners while data is loading
- โ ๏ธ Display errors when something goes wrong
Letโs explore how to use if-else, ternary, logical &&, and function-based conditionals inside JSX.
โ Traditional If-Else Doesnโt Work in JSX
// โ Invalid inside JSX return
return (
if (loggedIn) {
<p>Welcome!</p>
} else {
<p>Please log in.</p>
}
);
This throws a syntax error โ because JSX is just syntactic sugar for JavaScript function calls.
โ 1. Using Ternary Operator (? :)
The most common and clean way to conditionally render in JSX is using a ternary expression:
const isLoggedIn = true;
return (
<div>
{isLoggedIn ? <p>Welcome back!</p> : <p>Please log in.</p>}
</div>
);
๐ Use this when you have two options to toggle between.
โ 2. Logical AND (&&) for Single Conditions
If you only want to show something conditionally and show nothing otherwise, use &&
:
const hasMessages = true;
return (
<div>
{hasMessages && <p>๐ฉ You have new messages!</p>}
</div>
);
๐ This is cleaner than ternary if you donโt need an โelseโ case.
โ 3. Storing Logic Outside JSX
You can write a full if-else
block outside the return statement:
let content;
if (isLoading) {
content = <p>Loading...</p>;
} else if (error) {
content = <p>Something went wrong!</p>;
} else {
content = <p>Data loaded successfully.</p>;
}
return <div>{content}</div>;
๐ This is great for complex logic โ keeps JSX clean and readable.
โ 4. Using Functions for Complex Logic
You can create a helper function for large conditional blocks:
const renderUserStatus = () => {
if (loading) return <p>Loading user...</p>;
if (!user) return <p>User not found.</p>;
return <p>๐ค Welcome, {user.name}</p>;
};
return <div>{renderUserStatus()}</div>;
๐ Ideal when rendering varies based on multiple conditions or nested logic.
โ ๏ธ Avoid Overusing Nested Ternaries
// ๐ต Confusing to read
{isLoggedIn ? (isAdmin ? <p>Admin Panel</p> : <p>User Dashboard</p>) : <p>Please log in</p>}
Tip: Split it into a helper function or use if
blocks outside JSX for better readability.
๐ง Recap
- โ JSX doesnโt allow raw if-else inside return
- โ
Use ternary
? :
for two-choice conditions - โ
Use logical AND
&&
when thereโs no else condition - โ Use variables/functions outside JSX for complex logic
Clean conditional rendering makes your UI more dynamic and responsive without turning your JSX into spaghetti code! ๐
๐ Whatโs Next?
- ๐ Learn conditional class rendering with
classnames
- ๐ Explore state-based toggling (like themes or tabs)
- ๐๏ธ Build custom UI components like loaders or alerts based on state
Now that you understand conditionals in JSX, your React apps can adapt to anything users throw at them! ๐ฅ
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments