Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

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.

Remember: With 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'}
LogicalAndSyntax.jsx JSX
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

Browser 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.

CIIT TRAINING INSTITUTE — LOGICAL AND OPERATOR
Check Condition isLoggedIn === true?
↓
TRUE
↓
Display Element Welcome Back!
FALSE
↓
Skip Element No welcome message
Diagram Explanation: The right-side JSX element is rendered only when the condition on the left is truthy.

4. First Logical AND Example in React

Let's display a message when Samadhan has completed registration at CIIT Training Institute.

StudentRegistration.jsx JSX
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

Example 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.

StudentLogin.jsx JSX
function StudentLogin() {
    const isLoggedIn = true;
    const studentName = "Pradnya";

    return (
        <div>
            {isLoggedIn && (
                <h3>Welcome Back, {studentName}!</h3>
            )}
        </div>
    );
}

export default StudentLogin;
Try It Yourself
Welcome Back, Samadhan!
Try this: Turn off the login switch. The welcome message will disappear because the condition is false.

6. Logical AND Operator with Notifications

The logical AND operator can display a notification only when unread messages are available.

Notifications.jsx JSX
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

Example Output

My Dashboard

You have 3 unread messages.

Important: If 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.

CourseAvailability.jsx JSX
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

Example Output

React Course

React course is available for enrollment.

8. Common Mistakes

1. Displaying a zero accidentally

Incorrect.jsx JSX
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.

Correct Approach: Use an explicit comparison: 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.

Example.jsx JSX
{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 > 0 to avoid displaying zero accidentally.
  • Use the ternary operator when you need both true and false results.