Multiple Conditions in React
Learn how to handle multiple conditions in React using if-else-if statements, logical operators, and conditional JSX.
1. What Are Multiple Conditions?
Multiple conditions allow a React component to choose different outputs depending on several possible situations. For example, a student result may be Pass, Fail, or Distinction depending on the student's marks.
React uses JavaScript conditional statements and expressions to decide which JSX elements should be returned and displayed.
2. Using if-else-if Statements
The if-else-if statement checks conditions in order.
The first condition that evaluates to true determines which
block of code executes.
if (condition1) { ... }
else if (condition2) { ... }
else { ... }
function StudentResult() {
const marks = 85;
let result;
if (marks >= 90) {
result = "Distinction";
} else if (marks >= 40) {
result = "Pass";
} else {
result = "Fail";
}
return (
<div>
<h2>Student Result</h2>
<p>Marks: {marks}</p>
<p>Result: {result}</p>
</div>
);
}
export default StudentResult;
Code Explanation
marksstores the student's marks.- The first condition checks whether marks are at least 90.
- The second condition checks whether marks are at least 40.
- If neither condition is true, the result is Fail.
- The component displays the selected result using JSX.
Expected Output
Student Result
Marks: 85
Result: Pass
3. Flow Diagram of Multiple Conditions
The following diagram shows how React component logic can evaluate multiple conditions before choosing the output.
4. Using Multiple Conditions in JSX
The ternary operator can be nested when you need to choose between more than two possible outputs. However, deeply nested ternary expressions can become difficult to read.
function CourseStatus() {
const progress = 75;
return (
<div>
<h2>Course Progress</h2>
<p>
{progress === 100
? "Course Completed"
: progress >= 50
? "Course In Progress"
: "Course Not Started"}
</p>
</div>
);
}
export default CourseStatus;
Expected Output
Course Progress
Course In Progress
if-else-if statement or a separate function
instead of deeply nested ternary expressions.
5. Combining Conditions with Logical Operators
Logical operators allow a component to check more than one condition together.
function AdmissionStatus() {
const marks = 80;
const documentsSubmitted = true;
const isEligible =
marks >= 50 && documentsSubmitted;
return (
<div>
<h2>Admission Status</h2>
{isEligible
? <p>Eligible for Admission</p>
: <p>Not Eligible for Admission</p>}
</div>
);
}
export default AdmissionStatus;
Code Explanation
&&means both conditions must be true.- The marks must be at least 50.
- The required documents must be submitted.
- If both conditions are true, the admission message appears.
Expected Output
Admission Status
Eligible for Admission
6. Multiple Conditions Using Element Variables
Element variables allow us to prepare different JSX elements and select one using conditional statements.
function AttendanceStatus() {
const attendance = 82;
let status;
if (attendance >= 90) {
status = <h3>Excellent Attendance</h3>;
} else if (attendance >= 75) {
status = <h3>Good Attendance</h3>;
} else {
status = <h3>Attendance Needs Improvement</h3>;
}
return (
<div>
<h2>Attendance Report</h2>
<p>Attendance: {attendance}%</p>
{status}
</div>
);
}
export default AttendanceStatus;
Expected Output
Attendance Report
Attendance: 82%
Good Attendance
7. Common Mistakes to Avoid
1. Using conditions in the wrong order
Incorrect:
if (marks >= 40) {
result = "Pass";
} else if (marks >= 90) {
result = "Distinction";
}
A mark of 95 satisfies marks >= 40,
so the first branch executes. The Distinction condition
is never reached for those marks.
Correct:
if (marks >= 90) {
result = "Distinction";
} else if (marks >= 40) {
result = "Pass";
} else {
result = "Fail";
}
2. Forgetting the final else
When all conditions are false, a variable may remain
undefined if no default value or final else
is provided. Add a suitable fallback when every case
needs an output.
8. Practice Tasks
Complete the following exercises using React:
- Create a result component that displays Distinction, Pass, or Fail based on marks.
- Create a temperature component that displays Cold, Moderate, or Hot based on temperature.
- Create an admission component that checks marks and document submission.
- Create a course progress component that displays Not Started, In Progress, or Completed.
- Create an attendance component that displays different messages for different attendance percentages.
Summary
Multiple conditions in React help components display different content based on values and situations. The if-else-if statement is useful when several conditions must be checked in order. Ternary operators can select between expressions, while logical operators combine conditions. Element variables can store different JSX elements and render the appropriate one. Checking conditions in the correct order and providing a suitable fallback makes conditional rendering easier to understand and maintain.