Logical AND Operator in React
Learn how to use the logical AND (&&) operator to display content only when a condition is true, with simple React examples from CIIT Training Institute.
1. What Is the Logical AND Operator?
The logical AND operator (&&) is used to
check conditions. In React, it is commonly used to display
an element only when a condition is true.
For example, CIIT Training Institute can display a "Welcome Back!" message only when a student is logged in. If the student is not logged in, the message is not displayed.
condition && element, React displays
the element when the condition is truthy. When the condition
is false, the element is not displayed.
2. Logical AND Operator Syntax
The syntax used in JSX is:
{'condition && element'}
function WelcomeMessage() {
const isLoggedIn = true;
return (
<div>
{isLoggedIn && <h2>Welcome Back!</h2>}
</div>
);
}
export default WelcomeMessage;
Code Explanation
const isLoggedIn = true;
Stores the user's login status.
&&
Displays the element on the right when the left condition is truthy.
<h2>Welcome Back!</h2>
This heading is displayed because the condition is true.
Expected Output
Welcome Back!
3. Logical AND Flow Diagram
The condition is checked first. If it is true, the element is displayed. If it is false, the element is skipped.
4. First Logical AND Example in React
Let's display a message when Samadhan has completed registration at CIIT Training Institute.
function StudentRegistration() {
const isRegistered = true;
return (
<div>
<h2>Student Registration</h2>
{isRegistered && (
<p>Samadhan, your registration is complete!</p>
)}
</div>
);
}
export default StudentRegistration;
Simple Explanation
isRegistered = true
The student is registered.
isRegistered && (...)
The registration message is displayed because the condition is true.
isRegistered = false
If the value becomes false, the registration message is skipped.
Expected Browser Output
Student Registration
Samadhan, your registration is complete!
5. Interactive Example: Student Login
Select a student and change the login status. The welcome message will appear only when the student is logged in.
function StudentLogin() {
const isLoggedIn = true;
const studentName = "Pradnya";
return (
<div>
{isLoggedIn && (
<h3>Welcome Back, {studentName}!</h3>
)}
</div>
);
}
export default StudentLogin;
6. Logical AND Operator with Notifications
The logical AND operator can display a notification only when unread messages are available.
function Notifications() {
const unreadMessages = 3;
return (
<div>
<h2>My Dashboard</h2>
{unreadMessages > 0 && (
<p>You have {unreadMessages} unread messages.</p>
)}
</div>
);
}
export default Notifications;
Expected Browser Output
My Dashboard
You have 3 unread messages.
unreadMessages is zero, the condition
unreadMessages > 0 is false, so the notification
paragraph is not displayed.
7. Logical AND Operator with Course Availability
A course message can be displayed only when a course is available.
function CourseAvailability() {
const isAvailable = true;
return (
<div>
<h2>React Course</h2>
{isAvailable && (
<p>React course is available for enrollment.</p>
)}
</div>
);
}
export default CourseAvailability;
Expected Browser Output
React Course
React course is available for enrollment.
8. Common Mistakes
1. Displaying a zero accidentally
const unreadMessages = 0;
return (
<div>
{unreadMessages && <p>New messages available</p>}
</div>
);
In JavaScript, zero is falsy, but the expression
0 && element evaluates to 0.
React can display that zero on the page.
unreadMessages > 0 && element.
2. Expecting an else message
The logical AND operator does not provide an alternative message when the condition is false. Use the ternary operator when you need both true and false results.
{isLoggedIn
? <p>Welcome Back!</p>
: <p>Please Log In.</p>}
3. Forgetting JSX braces
Write JavaScript conditions inside curly braces when placing expressions in JSX.
9. Logical AND vs Ternary Operator
| Feature | Logical AND (&&) | Ternary (? :) |
|---|---|---|
| Purpose | Display content when a condition is truthy. | Choose between two results. |
| Syntax | condition && element |
condition ? A : B |
| When false | Does not render the right-side element. | Returns the false-side result. |
| Example | Show unread notifications. | Show Pass or Fail. |
10. Practice Tasks
Task 1: Student Login
Create a component for Samadhan that displays "Welcome Back!" only when the student is logged in.
Task 2: Course Registration
Create a component for Pradnya that displays a registration confirmation when registration is complete.
Task 3: Notifications
Create a component for Samiksha that displays a message when the unread notification count is greater than zero.
Task 4: Course Availability
Display "Course Available" only when the course availability variable is true.
Summary
- The logical AND operator is written as
&&. - It is used for conditional rendering in React.
- The right-side element is rendered when the condition is truthy.
- It does not provide an alternative message when the condition is false.
- Use comparisons such as
unreadMessages > 0to avoid displaying zero accidentally. - Use the ternary operator when you need both true and false results.