Ternary Operator in React
Learn how to use the ternary operator to display different content based on conditions, with simple React examples from CIIT Training Institute.
1. What Is the Ternary Operator?
The ternary operator is a short way to choose between two values based on a condition. In React, it is commonly used inside JSX to display different messages or elements.
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.
2. Ternary Operator Syntax
The syntax of the ternary operator is:
condition ? valueIfTrue : valueIfFalse
const age = 20;
const message = age >= 18
? "You are eligible."
: "You are not eligible.";
console.log(message);
Code Explanation
age >= 18
Checks whether the age is at least 18.
?
Separates the condition from the true value.
"You are eligible."
This value is selected when the condition is true.
:
Separates the true value from the false value.
Expected Output
You are eligible.
3. Ternary Operator Flow Diagram
The condition decides which result is selected. Only one of the two values is used.
4. First Ternary Operator Example in React
Let's display a message based on whether Samadhan is registered for a course at CIIT Training Institute.
function StudentWelcome() {
const isRegistered = true;
return (
<div>
{isRegistered
? <h2>Welcome, Samadhan!</h2>
: <h2>Please register for a course.</h2>}
</div>
);
}
export default StudentWelcome;
Simple Explanation
const isRegistered = true;
Stores the student's registration status.
isRegistered ? ... : ...
Chooses which heading should be displayed.
<h2>Welcome, Samadhan!</h2>
Appears when the student is registered.
<h2>Please register for a course.</h2>
Appears when the student is not registered.
Expected Browser Output
Welcome, Samadhan!
5. Interactive Example: Student Registration
Select a student and change the registration status. The message will change according to the condition.
function RegistrationMessage() {
const isRegistered = false;
const studentName = "Pradnya";
return (
<div>
{isRegistered
? <p>Welcome, {studentName}!</p>
: <p>Please register, {studentName}.</p>}
</div>
);
}
export default RegistrationMessage;
Expected Output
Welcome, Samadhan!
6. Ternary Operator with Marks
The ternary operator can also select a result based on a student's marks. Here, marks of 40 or more mean Pass.
function StudentResult() {
const marks = 75;
return (
<div>
{marks >= 40
? <h3>Congratulations! You passed.</h3>
: <h3>You failed. Please try again.</h3>}
</div>
);
}
export default StudentResult;
7. Ternary Operator with Course Availability
A ternary operator can select a message based on whether a course is available.
function CourseAvailability() {
const isAvailable = true;
return (
<div>
{isAvailable
? <p>React course is available.</p>
: <p>React course is currently unavailable.</p>}
</div>
);
}
export default CourseAvailability;
Expected Browser Output
React course is available.
8. Common Mistakes
1. Forgetting the colon
// Incorrect
isRegistered ? "Welcome" "Please register"
isRegistered ? "Welcome" : "Please register".
2. Using a ternary operator for too many conditions
Nested ternary expressions can become difficult to read. For complex conditions, use if-else statements or another clear structure.
3. Using an if statement directly inside JSX braces
An ordinary if statement is a JavaScript statement, not an expression that can be placed directly inside JSX braces. Use a ternary expression or prepare the result before the return statement.
9. If-Else vs Ternary Operator
| Feature | If-Else | Ternary Operator |
|---|---|---|
| Syntax | if / else |
? : |
| Usage | Useful for longer logic. | Useful for short two-way choices. |
| Inside JSX | Use before the return statement. | Can be used inside JSX braces. |
| Example | Complex result checking. | Showing a welcome message. |
10. Practice Tasks
Task 1: Student Registration
Create a component for Samadhan that displays a welcome message if registered and a registration message otherwise.
Task 2: Student Result
Create a component for Pradnya that uses the ternary operator to display Pass or Fail based on marks.
Task 3: Course Availability
Create a component for Samiksha that displays whether a React course is available.
Task 4: Login Message
Create a component that displays "Welcome Back!" when the user is logged in and "Please Log In" otherwise.
Summary
- The ternary operator is a short way to choose between two values.
- Its syntax is
condition ? trueValue : falseValue. - The question mark separates the condition from the true value.
- The colon separates the true value from the false value.
- It can be used inside JSX braces to display conditional content.
- Use if-else when the logic becomes complex or difficult to read.