JSX Conditional Rendering ? : Operator
The ternary operator (? :) is a compact way to perform conditional rendering in React JSX. It behaves like an inline if-else and is ideal when you want to toggle between two UI outputs directly inside JSX.
General syntax:
condition ? expressionIfTrue : expressionIfFalse
If condition is true, React evaluates and renders expressionIfTrue, otherwise it renders expressionIfFalse.
Basic form in JavaScript:
// Returns valueA when condition is true, otherwise valueB
condition ? valueA : valueB
In React JSX:
{ } in JSX.Use a ternary to decide whether to show a Login or Logout button based on isLoggedIn.
// Conditionally render Login or Logout button
function LoginButton({ isLoggedIn }) {
return (
<div>
{isLoggedIn ? (
<button className="btn btn-danger">Logout</button>
) : (
<button className="btn btn-primary">Login</button>
)}
</div>
);
}
If isLoggedIn is true, React renders the red Logout button. If it is false, it renders the blue Login button instead.
You can use the ternary operator for simple text messages or full JSX content based on a value like user.
// Show personalized greeting when user is present
function Greeting({ user }) {
return (
<h4>
{user ? `Welcome, ${user}! ?` : "Welcome, Guest!"}
</h4>
);
}
user has a value (like "Alex"), the greeting becomes Welcome, Alex! ?.user is null or undefined, React shows "Welcome, Guest!".Use ternaries to switch between entire components, such as admin and user views.
// Swap whole components based on isAdmin flag
function AdminPanel() {
return <h5 className="text-danger">Admin Panel</h5>;
}
function UserPanel() {
return <h5 className="text-success">User Panel</h5>;
}
function Dashboard({ isAdmin }) {
return <div>{isAdmin ? <AdminPanel /> : <UserPanel />}</div>;
}
When isAdmin is true, the admin view is rendered; otherwise, the user view appears. This is common in dashboards, role-based views, and login-protected sections.
Nested ternaries allow multiple states (like loading, error, success), but they must be used carefully to avoid confusing code.
// Handle loading, error and success states with nested ternary
function StatusIndicator({ status }) {
return (
<p>
{status === "loading"
? "⏳ Loading..."
: status === "error"
? "❌ Error occurred!"
: "✅ Success!"}
</p>
);
}
"loading" ⟶ shows ⏳ Loading..."error" ⟶ shows ❌ Error occurred!If conditions grow more complex, move this logic into a separate function or replace with a clean if-else chain.
Use ternary when you need to choose between two alternatives. Use && when you only want to conditionally render something when the condition is true.
// Compare logical AND and ternary in JSX
// Using &&
{isAdmin && <p>You are an Admin</p>}
// Using ternary
{isAdmin ? <p>Welcome Admin</p> : <p>Welcome User</p>}
isAdmin && <p>You are an Admin</p> only renders the paragraph when isAdmin is true.Ternaries can dynamically pick classes, inline styles, or labels for theming and mode switches.
// Toggle theme styles and label with a ternary
function ThemeSwitcher({ darkMode }) {
return (
<div
className={darkMode ? "bg-dark text-light p-3" : "bg-light text-dark p-3"}
>
{darkMode ? "? Dark Mode" : "☀️ Light Mode"}
</div>
);
}
The container switches both its classes and label based on the value of darkMode, which is a common pattern in theme toggles and mode banners.
( ) for each branch to keep structure clear.if-else or a separate component.isLoggedIn.status prop.if-else logic. Compare which version is easier to read.Goal: Become comfortable using the ternary (? :) operator to render clear, concise, and efficient React JSX for alternative UI states.