# Conditional Rendering

> How to render different UI elements depending on state or props using JavaScript.

Subject: React | Lesson 11 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/conditional-rendering
Previous: [Event Handling](https://codewithanimation.in/react/event-handling.md) | Next: [Lists & Keys](https://codewithanimation.in/react/lists-and-keys.md)

## What it is

Conditional rendering in React works exactly the same way conditions work in JavaScript.

Because React components are just functions, you can use standard JavaScript like `if` statements, ternary operators (`? :`), and logical AND (`&&`) to return different JSX depending on the current state.

## Why it matters

Almost every application requires dynamic UI. You need to show a 'Login' button for guests, but a 'Profile' picture for authenticated users. You need to show a spinner while data loads, and a list when it's done.

React doesn't invent new template languages for this (like `ng-if` or `v-if`). It relies purely on native JavaScript.

## The three main approaches

**1. Early Return:** Use a standard `if` statement *before* your final `return` to exit the component entirely: `if (isLoading) return <Spinner />;`

**2. Ternary Operator:** Use this inside JSX when you need to choose between two elements: `{isLoggedIn ? <Admin /> : <Guest />}`

**3. Logical AND:** Use this inside JSX when you want to render something OR nothing: `{unreadMessages > 0 && <Badge />}`

## Common mistake: The '0' rendering trap

When using the Logical AND (`&&`), JavaScript evaluates the left side. If it's falsy, it returns the left side.

If you write `{messages.length && <Badge />}` and the array is empty, the left side is `0`. JavaScript returns `0`, and React will literally render the number '0' onto your screen!

Always ensure the left side evaluates to a boolean: `{messages.length > 0 && <Badge />}`.

## Tip: You can't put 'if' inside JSX

JSX tags are just function calls in disguise (`React.createElement()`). You can't put an `if` statement inside a function call arguments list in JavaScript. That's why you must use expressions (like ternaries) inside JSX curly braces.

## Try it: The Three Techniques

Toggle the Admin switch to see how all three JavaScript conditional techniques update the UI simultaneously.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/conditional-rendering

## Check yourself

### Question 1

Why can't you write an `if` statement directly inside JSX, like `<div>{if (user) { return <Avatar /> }}</div>`?

- A. Because `if` is not supported in React.
- B. Because everything inside JSX `{}` must be a JavaScript expression that evaluates to a value, and `if` is a statement.
- C. Because you must import the `<If>` component from the React library first.

Correct answer: B

JSX compiles to function calls. You can't pass an `if` statement as an argument to a function, but you can pass the result of a ternary expression.

- Why not A: React fully supports `if` statements, just not inside JSX expressions.
- Why not C: React does not use special components for control flow; it relies on plain JavaScript.

### Question 2

What will render if you write `<div>{count && <Counter />}</div>` and `count` is `0`?

- A. An empty `<div></div>`
- B. A `<div>` containing the `<Counter />` component
- C. A `<div>` containing the number `0`

Correct answer: C

Because `0` is falsy, JavaScript short-circuits and returns the left side. React renders numbers, so it will print `0` to the screen. You should use `count > 0 && <Counter />` instead.

- Why not A: That happens if count is false, null, or undefined, but 0 is a number.
- Why not B: 0 is falsy, so it doesn't evaluate the right side.

### Question 3

Which operator is best for showing either a Login button or a Logout button depending on state, directly inside your JSX?

- A. The logical AND operator (`&&`)
- B. The ternary operator (`? :`)
- C. An early return `if` statement

Correct answer: B

The ternary operator `{isUser ? <Logout /> : <Login />}` cleanly chooses between two elements directly inside your JSX tree.

- Why not A: This is best for showing something OR nothing, not choosing between two things.
- Why not C: An early return replaces the entire component output; it cannot be used inline inside a larger JSX block.

## Remember this

- Use `if` statements for early returns to replace the entire component output.
- Use ternary operators (`? :`) inside JSX to choose between two options.
- Use logical AND (`&&`) inside JSX to render something conditionally or nothing at all.
- Be careful with numbers on the left side of `&&`—convert them to booleans to avoid rendering a `0`.
