Full answer
JSX has no special if or for syntax. You use ordinary JavaScript expressions, and React renders whatever they produce.
Outside the return, you can use if and early returns: if (loading) return <Spinner />;. This keeps the main layout readable.
Inside JSX, use a ternary for two outcomes, isAdmin ? <AdminMenu /> : <UserMenu />, and && when there is nothing to show in the other case, hasError && <ErrorText />. null, undefined, true and false render nothing, which makes this work.
The trap is && with numbers. In {items.length && <List />}, an empty array gives 0, and React renders the number 0 on screen, because 0 is a valid thing to display. Write items.length > 0 && <List /> or use a ternary.
For lists, call map and return elements: students.map((s) => <li key={s.id}>{s.name}</li>). Every element needs a key that is stable and unique among siblings. Use an id from the data. Filter first and then map if only some items should appear. An empty list should show an explicit empty state, since a blank area looks broken.
Do not put long logic inside the JSX. Prepare the array in a variable above the return, or extract a small component. Readable components usually have a short return statement.
The component below shows a loading branch, an empty branch and a list.
const students = [
{ id: 1, name: 'Asha', marks: 82 },
{ id: 2, name: 'Ravi', marks: 47 },
{ id: 3, name: 'Meera', marks: 91 },
];
function ResultList({ rows, loading }) {
if (loading) {
return <p>Loading results...</p>;
}
const passed = rows.filter((row) => row.marks >= 50);
return (
<section>
<h2>Passed students ({passed.length})</h2>
{passed.length > 0 ? (
<ul>
{passed.map((row) => (
<li key={row.id}>
{row.name} scored {row.marks}
</li>
))}
</ul>
) : (
<p>No one passed yet.</p>
)}
</section>
);
}
export default function App() {
return <ResultList rows={students} loading={false} />;
}The component returns early while loading, filters before mapping, and shows an empty-state message when there is nothing to list. Rendered result: <section><h2>Passed students (2)</h2><ul><li>Asha scored 82</li><li>Meera scored 91</li></ul></section>
Key points
- Use if with early return, a ternary or && for conditions. All are plain JavaScript expressions.
- null, undefined, true and false render nothing, but 0 and empty strings may render.
- Guard numeric conditions with a comparison such as length > 0.
- Render lists with map and give each element a stable key.
- Show a deliberate empty state instead of a blank area.
Common mistakes
- Writing {count && <Item />} and seeing a stray 0 on screen.
- Forgetting the key prop on the mapped element, or placing it on an inner element.
- Mapping with forEach, which returns undefined and renders nothing.
- Using nested ternaries that are hard to read, instead of early returns or small components.
Follow-ups
- Why does an empty array render nothing but the number 0 renders as text?
- How would you handle a very long list efficiently?
- How would you show loading, error and empty states for fetched data?
Tips
- Mention the 0 trap. It is a classic filter for real experience.
- Keep JSX short. Say that you move logic into variables or components.
- Always say "stable key" when talking about lists.