If-Else Rendering in React
Learn how to use if-else statements to display different content based on conditions in React, with simple examples from CIIT Training Institute.
1. What Is If-Else Rendering?
If-else rendering means displaying different UI content depending on whether a condition is true or false. React components can use normal JavaScript if-else statements to decide what JSX to return.
For example, CIIT Training Institute can display a welcome message when a student is registered and a registration message when the student is not registered.
if block runs.
Otherwise, the else block runs.
2. Basic If-Else Syntax
JavaScript uses the if statement to check a
condition and the else statement to handle
the alternative case.
if (condition) {
// Code runs when condition is true
} else {
// Code runs when condition is false
}
Simple Explanation
if (condition)
Checks whether the condition is true.
{ }
Contains the statements that should execute.
else
Runs when the if condition is false.
3. If-Else Flow Diagram
The condition decides which message or UI should be displayed. Only the matching branch is selected.
4. First React If-Else Example
Let's create a component that checks whether Samadhan is registered for a course at CIIT Training Institute.
function StudentStatus() {
const isRegistered = true;
if (isRegistered) {
return (
<h2>
Welcome to CIIT Training Institute, Samadhan!
</h2>
);
} else {
return (
<h2>
Please register for a course.
</h2>
);
}
}
export default StudentStatus;
Code Explanation
const isRegistered = true;
Stores the registration status. Here, the value is true.
if (isRegistered)
Checks whether Samadhan is registered.
return (...)
Returns the JSX that React should display.
else
Provides the alternative message when the condition is false.
Expected Browser Output
Welcome to CIIT Training Institute, Samadhan!
isRegistered = true to
isRegistered = false. The registration message
will be returned instead.
5. Interactive Example: Student Registration
Select a student and change the registration status to see how an if-else condition selects different messages.
function RegistrationStatus() {
const studentName = "Pradnya";
const isRegistered = false;
if (isRegistered) {
return <h3>Welcome, {studentName}!</h3>;
} else {
return <h3>Please register, {studentName}.</h3>;
}
}
export default RegistrationStatus;
Expected Output
Welcome to CIIT Training Institute, Samadhan!
6. If-Else with Marks
We can also use if-else to display a student's result. In this example, a student passes when the marks are greater than or equal to 40.
function StudentResult() {
const marks = 75;
if (marks >= 40) {
return <h3>Congratulations! You passed.</h3>;
} else {
return <h3>You failed. Please try again.</h3>;
}
}
export default StudentResult;
Interactive Result Checker
7. If-Else vs Ternary Operator
Both approaches can select content based on a condition. If-else is useful for longer logic, while the ternary operator is often convenient for short choices inside JSX.
| Feature | If-Else | Ternary Operator |
|---|---|---|
| Syntax | Uses if and else. |
Uses condition ? a : b. |
| Best for | Longer or complex logic. | Short two-way choices. |
| Inside JSX braces | A statement cannot be placed directly there. | A ternary expression can be used. |
| Readability | Clear for multiple statements. | Compact for simple conditions. |
8. Common Mistakes
1. Forgetting the else case
function Status() {
const isReady = false;
if (isReady) {
return <h3>Ready</h3>;
}
return <h3>Not ready</h3>;
}
This example is valid too: the second return handles the false case without an explicit else block.
2. Putting an if statement directly inside JSX
// Incorrect
return (
<div>
{if (isReady) { "Ready" }}
</div>
);
3. Using assignment instead of comparison
// Comparison
if (marks >= 40) {
// Pass
} else {
// Fail
}
Use a comparison such as === or
>= when checking a condition.
9. Practice Tasks
Task 1: Student Registration
Create a component that checks whether Samadhan is registered for a React course.
- Create an
isRegisteredvariable. - Use if-else to check the value.
- Display a welcome or registration message.
Task 2: Student Result
Create a result component for Pradnya.
- Store marks in a variable.
- Display Pass if marks are at least 40.
- Display Fail otherwise.
Task 3: Course Availability
Create a component for Samiksha that displays whether a course is available.
- Create a Boolean variable named
isAvailable. - Use if-else to select the message.
- Display the appropriate message for each condition.
Summary
- If-else rendering selects UI based on a condition.
- The if block runs when its condition is true.
- The else block runs when the condition is false.
- React components can return different JSX for different conditions.
- Use if-else before the return statement for clear conditional logic.
- Use the ternary operator for compact two-way expressions inside JSX.