🎯 What Is Multi-Option Conditional Rendering? (Simple Analogy)

Imagine you're a teacher giving out stickers based on grades:

TERNARY OPERATOR (Only 2 choices):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚Β  πŸŽ“ STICKER SYSTEM (Pass/Fail)Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  Did you pass?Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  YES β†’ 🌟 Gold StarΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  NOΒ  β†’ πŸ“„ Try Again NoteΒ Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  In JavaScript:Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  const sticker = passed ? "🌟" : "πŸ“„";Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  Problem: What about different levels?Β  β”‚
β”‚Β  Beginner, Intermediate, Advanced?Β Β Β Β Β  β”‚
β”‚Β  Ternary only handles YES/NO!Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  In React:Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  {passed ? <GoldStar /> : <TryAgain />} β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

&& OPERATOR WITH MULTIPLE OPTIONS (3+ choices):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚Β  πŸŽ“ SMART STICKER SYSTEM (3 Levels)Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  Grade = Beginner?Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  YES β†’ 🌱 Sprout StickerΒ Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  NOΒ  β†’ (skip, check next)Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  Grade = Intermediate?Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  YES β†’ 🌿 Leaf StickerΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  NOΒ  β†’ (skip, check next)Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  Grade = Advanced?Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  YES β†’ 🌳 Tree StickerΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β  NOΒ  β†’ (skip, nothing shows)Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  In React:Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  {level === "beginner" && "🌱"}Β Β Β Β Β Β Β Β  β”‚
β”‚Β  {level === "intermediate" && "🌿"}Β Β Β Β  β”‚
β”‚Β  {level === "advanced" && "🌳"}Β Β Β Β Β Β Β Β  β”‚
β”‚Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  KEY INSIGHT:Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  β”‚
β”‚Β  Only ONE will match! Like a switch.Β Β Β  β”‚
β”‚Β  The others "short circuit" and skip.Β Β  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

⚠️ The Big Problem: "Ternary Only Has 2 Options!"

// ==========================================
// THE "TERNARY LIMITATION" TRAP
// ==========================================

// ❌ WRONG: Nested ternaries for 3+ options (MESSY!)
function Skill({ level }) {
  return (
    <div>
      {/* Nested ternaries = SPAGHETTI CODE! */}
      {level === "beginner"
        ? "πŸ‘Ά"
        : level === "intermediate"
          ? "πŸ’ͺ"
          : level === "advanced"
            ? "πŸ”₯"
            : "❓"
      }
    </div>
  );
}

// Problems:
// 1. Hard to read - like a staircase!
// 2. Easy to mess up the nesting
// 3. Adding a 4th option is painful
// 4. Looks like this:
//    condition ? A
//      : condition ? B
//        : condition ? C
//          : D

// Visual:
// level = "intermediate"
//    ↓
// level === "beginner" ? NO β†’ go to else
//    ↓
// level === "intermediate" ? YES β†’ return "πŸ’ͺ"
//    ↓
// "πŸ’ͺ"

// But look at the code! It's a mess!

// ==========================================
// THE SOLUTION: Multiple && Operators
// ==========================================

// βœ… CLEAN: Each condition on its own line
function Skill({ level }) {
  return (
    <div>
      {/* Each condition is SEPARATE and CLEAR */}
      {level === "beginner" && "πŸ‘Ά"}
      {level === "intermediate" && "πŸ’ͺ"}
      {level === "advanced" && "πŸ”₯"}
    </div>
  );
}

// Benefits:
// 1. Flat structure - no nesting!
// 2. Easy to read - like a list
// 3. Easy to add new options
// 4. Each line does ONE thing

// Visual:
// level = "intermediate"
//    ↓
// {level === "beginner" && "πŸ‘Ά"}
//    ↓
// false && "πŸ‘Ά" β†’ false (short circuits, renders nothing)
//    ↓
// {level === "intermediate" && "πŸ’ͺ"}
//    ↓
// true && "πŸ’ͺ" β†’ "πŸ’ͺ" (renders!)
//    ↓
// {level === "advanced" && "πŸ”₯"}
//    ↓
// false && "πŸ”₯" β†’ false (short circuits, renders nothing)
//    ↓
// Result: "πŸ’ͺ"

πŸ“‹ Complete Basic Examples

Create file: react-multi-conditional-rendering.js

// ==========================================
// MULTI-OPTION CONDITIONAL RENDERING
// Using && Operator for 3+ Choices
// ==========================================

/*
PATTERN FOR MULTIPLE OPTIONS:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Instead of nested ternaries:             β”‚
β”‚  {condition1 ? A : condition2 ? B : C}  β”‚
β”‚                                           β”‚
β”‚  Use multiple && operators:               β”‚
β”‚  {condition1 && A}                        β”‚
β”‚  {condition2 && B}                        β”‚
β”‚  {condition3 && C}                        β”‚
β”‚                                           β”‚
β”‚  Only ONE will be true and render!       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
*/

// ==========================================
// EXAMPLE 1: Skill Level Emojis (The Challenge)
// ==========================================

function Skill({ skill, color, level }) {
  return (
    <div className="skill" style={{ backgroundColor: color }}>
      <span>{skill}</span>
      
      {/* MULTIPLE && CONDITIONS */}
      {/* Only ONE will match the level! */}
      {level === "beginner" && "πŸ‘Ά"}
      {level === "intermediate" && "πŸ’ͺ"}
      {level === "advanced" && "πŸ”₯"}
      
      <span>{level}</span>
    </div>
  );
}

// Usage:
function App() {
  const skills = [
    { skill: "HTML+CSS", level: "advanced", color: "#2662EA" },
    { skill: "JavaScript", level: "advanced", color: "#EFD81D" },
    { skill: "Web Design", level: "intermediate", color: "#C3DCAF" },
    { skill: "Git and GitHub", level: "intermediate", color: "#E84F33" },
    { skill: "React", level: "beginner", color: "#60DAFB" },
    { skill: "Svelte", level: "beginner", color: "#FF3B00" }
  ];

  return (
    <div className="skill-list">
      {skills.map((skill) => (
        <Skill
          key={skill.skill}
          skill={skill.skill}
          color={skill.color}
          level={skill.level}
        />
      ))}
    </div>
  );
}

// Visual Flow for "React" skill (beginner):
// Skill component receives: skill="React", color="#60DAFB", level="beginner"
//    ↓
// {level === "beginner" && "πŸ‘Ά"}
// "beginner" === "beginner" β†’ true
// true && "πŸ‘Ά" β†’ "πŸ‘Ά" βœ“ RENDERS!
//    ↓
// {level === "intermediate" && "πŸ’ͺ"}
// "beginner" === "intermediate" β†’ false
// false && "πŸ’ͺ" β†’ false βœ— SKIPPED!
//    ↓
// {level === "advanced" && "πŸ”₯"}
// "beginner" === "advanced" β†’ false
// false && "πŸ”₯" β†’ false βœ— SKIPPED!
//    ↓
// Result: React πŸ‘Ά beginner

// Visual Flow for "JavaScript" skill (advanced):
//    ↓
// {level === "beginner" && "πŸ‘Ά"}
// "advanced" === "beginner" β†’ false βœ— SKIPPED!
//    ↓
// {level === "intermediate" && "πŸ’ͺ"}
// "advanced" === "intermediate" β†’ false βœ— SKIPPED!
//    ↓
// {level === "advanced" && "πŸ”₯"}
// "advanced" === "advanced" β†’ true
// true && "πŸ”₯" β†’ "πŸ”₯" βœ“ RENDERS!
//    ↓
// Result: JavaScript πŸ”₯ advanced

// ==========================================
// EXAMPLE 2: Traffic Light System
// ==========================================

function TrafficLight({ status }) {
  return (
    <div className="traffic-light">
      {/* RED: Stop */}
      {status === "stop" && (
        <div className="light red active">πŸ”΄ STOP</div>
      )}
      
      {/* YELLOW: Caution */}
      {status === "caution" && (
        <div className="light yellow active">🟑 CAUTION</div>
      )}
      
      {/* GREEN: Go */}
      {status === "go" && (
        <div className="light green active">🟒 GO</div>
      )}
    </div>
  );
}

// Usage:
// <TrafficLight status="stop" />     β†’ Shows πŸ”΄ STOP
// <TrafficLight status="caution" />  β†’ Shows 🟑 CAUTION
// <TrafficLight status="go" />       β†’ Shows 🟒 GO

// ==========================================
// EXAMPLE 3: User Role Badge
// ==========================================

function UserBadge({ role }) {
  return (
    <span className="badge">
      {role === "admin" && "πŸ‘‘ Administrator"}
      {role === "moderator" && "πŸ›‘οΈ Moderator"}
      {role === "user" && "πŸ‘€ User"}
      {role === "guest" && "πŸ‘‹ Guest"}
    </span>
  );
}

// ==========================================
// EXAMPLE 4: Weather Icon
// ==========================================

function WeatherIcon({ condition }) {
  return (
    <div className="weather">
      {condition === "sunny" && "β˜€οΈ"}
      {condition === "cloudy" && "☁️"}
      {condition === "rainy" && "🌧️"}
      {condition === "stormy" && "β›ˆοΈ"}
      {condition === "snowy" && "❄️"}
      {condition === "windy" && "πŸ’¨"}
    </div>
  );
}

// ==========================================
// EXAMPLE 5: Notification Type
// ==========================================

function Notification({ type, message }) {
  return (
    <div className={`notification ${type}`}>
      {type === "success" && "βœ…"}
      {type === "error" && "❌"}
      {type === "warning" && "⚠️"}
      {type === "info" && "ℹ️"}
      <span>{message}</span>
    </div>
  );
}

// Usage:
// <Notification type="success" message="Saved!" />     β†’ βœ… Saved!
// <Notification type="error" message="Failed!" />      β†’ ❌ Failed!
// <Notification type="warning" message="Careful!" />   β†’ ⚠️ Careful!
// <Notification type="info" message="Did you know?" /> β†’ ℹ️ Did you know?

πŸš€ Interactive React Usage Examples

Complete React File: MultiConditionalRenderingMasterClass.jsx

import React, { useState } from 'react';

// ==========================================
// INTERACTIVE MULTI-OPTION CONDITIONAL RENDERING
// ==========================================

function MultiConditionalRenderingMasterClass() {
  const [activeDemo, setActiveDemo] = useState('skills');

  const demos = {
    skills: { title: 'Skill Levels', component: <SkillsDemo /> },
    traffic: { title: 'Traffic Light', component: <TrafficDemo /> },
    weather: { title: 'Weather Icons', component: <WeatherDemo /> },
    comparison: { title: 'vs Nested Ternary', component: <ComparisonDemo /> }
  };

  return (
    <div style={{ maxWidth: '900px', margin: '0 auto', padding: '20px', fontFamily: 'Arial, sans-serif' }}>
      <header style={{ background: '#e9c46a', color: '#264653', padding: '30px', borderRadius: '10px', marginBottom: '30px' }}>
        <h1 style={{ margin: 0 }}>πŸ”€ Multi-Option Conditional Rendering</h1>
        <p style={{ margin: '10px 0 0 0', opacity: 0.9 }}>Using && for 3+ Choices (Like a Switch)</p>
      </header>

      <nav style={{ display: 'flex', gap: '10px', marginBottom: '30px', flexWrap: 'wrap' }}>
        {Object.entries(demos).map(([key, { title }]) => (
          <button
            key={key}
            onClick={() => setActiveDemo(key)}
            style={{
              padding: '12px 24px',
              fontSize: '16px',
              border: 'none',
              borderRadius: '8px',
              cursor: 'pointer',
              backgroundColor: activeDemo === key ? '#264653' : '#e0e0e0',
              color: activeDemo === key ? 'white' : '#333',
              fontWeight: activeDemo === key ? 'bold' : 'normal'
            }}
          >
            {title}
          </button>
        ))}
      </nav>

      <main style={{ background: '#f8f9fa', padding: '30px', borderRadius: '10px', minHeight: '400px' }}>
        {demos[activeDemo].component}
      </main>
    </div>
  );
}

// ==========================================
// DEMO 1: Skill Levels (The Challenge Solution)
// ==========================================

function SkillsDemo() {
  const [skills] = useState([
    { skill: "HTML+CSS", level: "advanced", color: "#2662EA" },
    { skill: "JavaScript", level: "advanced", color: "#EFD81D" },
    { skill: "Web Design", level: "intermediate", color: "#C3DCAF" },
    { skill: "Git and GitHub", level: "intermediate", color: "#E84F33" },
    { skill: "React", level: "beginner", color: "#60DAFB" },
    { skill: "Svelte", level: "beginner", color: "#FF3B00" }
  ]);

  return (
    <div>
      <h2>Developer Skills with Level Emojis</h2>
      
      <div style={{ marginBottom: '20px', padding: '15px', background: '#e3f2fd', borderRadius: '8px' }}>
        <h4>Challenge Requirements:</h4>
        <ul style={{ margin: '5px 0' }}>
          <li>Create array of skills with name, level, and color</li>
          <li>Map over array to render Skill components</li>
          <li>Conditionally show emoji based on level (3 options)</li>
        </ul>
      </div>

      <div style={{ display: 'grid', gap: '10px' }}>
        {skills.map((s) => (
          <SkillCard key={s.skill} skill={s.skill} color={s.color} level={s.level} />
        ))}
      </div>

      <div style={{ marginTop: '20px', padding: '15px', background: '#fff3e0', borderRadius: '8px' }}>
        <h4>Code Pattern:</h4>
        <pre style={{ background: '#1e1e1e', color: '#d4d4d4', padding: '15px', borderRadius: '4px', fontSize: '13px' }}>
{`function Skill({ skill, color, level }) {
  return (
    <div style={{ backgroundColor: color }}>
      <span>{skill}</span>
      {level === "beginner" && "πŸ‘Ά"}
      {level === "intermediate" && "πŸ’ͺ"}
      {level === "advanced" && "πŸ”₯"}
      <span>{level}</span>
    </div>
  );
}`}
        </pre>
      </div>
    </div>
  );
}

function SkillCard({ skill, color, level }) {
  return (
    <div
      style={{
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'space-between',
        padding: '15px 20px',
        backgroundColor: color,
        borderRadius: '8px',
        color: level === 'advanced' ? 'white' : '#333'
      }}
    >
      <span style={{ fontWeight: 'bold', fontSize: '16px' }}>{skill}</span>
      <div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>
        <span style={{ fontSize: '24px' }}>
          {level === "beginner" && "πŸ‘Ά"}
          {level === "intermediate" && "πŸ’ͺ"}
          {level === "advanced" && "πŸ”₯"}
        </span>
        <span style={{
          textTransform: 'capitalize',
          fontSize: '14px',
          backgroundColor: 'rgba(255,255,255,0.3)',
          padding: '4px 12px',
          borderRadius: '12px'
        }}>
          {level}
        </span>
      </div>
    </div>
  );
}

// ==========================================
// DEMO 2: Traffic Light
// ==========================================

function TrafficDemo() {
  const [status, setStatus] = useState('stop');

  return (
    <div>
      <h2>Traffic Light System</h2>
      
      <div style={{ marginBottom: '20px', display: 'flex', gap: '10px' }}>
        {['stop', 'caution', 'go'].map(s => (
          <button
            key={s}
            onClick={() => setStatus(s)}
            style={{
              padding: '10px 20px',
              background: status === s
                ? s === 'stop' ? '#f44336' : s === 'caution' ? '#ff9800' : '#4caf50'
                : '#e0e0e0',
              color: status === s ? 'white' : '#333',
              border: 'none',
              borderRadius: '5px',
              textTransform: 'capitalize'
            }}
          >
            {s}
          </button>
        ))}
      </div>

      <div style={{
        width: '200px',
        margin: '0 auto',
        padding: '20px',
        background: '#333',
        borderRadius: '20px',
        display: 'flex',
        flexDirection: 'column',
        gap: '15px',
        alignItems: 'center'
      }}>
        {/* STOP */}
        {status === "stop" && (
          <div style={{
            width: '100px',
            height: '100px',
            borderRadius: '50%',
            backgroundColor: '#f44336',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: '40px',
            boxShadow: '0 0 30px #f44336'
          }}>
            πŸ”΄
          </div>
        )}
        
        {/* CAUTION */}
        {status === "caution" && (
          <div style={{
            width: '100px',
            height: '100px',
            borderRadius: '50%',
            backgroundColor: '#ff9800',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: '40px',
            boxShadow: '0 0 30px #ff9800'
          }}>
            🟑
          </div>
        )}
        
        {/* GO */}
        {status === "go" && (
          <div style={{
            width: '100px',
            height: '100px',
            borderRadius: '50%',
            backgroundColor: '#4caf50',
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: '40px',
            boxShadow: '0 0 30px #4caf50'
          }}>
            🟒
          </div>
        )}
      </div>

      <div style={{ marginTop: '20px', textAlign: 'center', fontSize: '18px', fontWeight: 'bold' }}>
        {status === "stop" && "STOP! πŸ›‘"}
        {status === "caution" && "CAUTION ⚠️"}
        {status === "go" && "GO! πŸš€"}
      </div>
    </div>
  );
}

// ==========================================
// DEMO 3: Weather Icons
// ==========================================

function WeatherDemo() {
  const [weather, setWeather] = useState('sunny');

  const weatherTypes = ['sunny', 'cloudy', 'rainy', 'stormy', 'snowy', 'windy'];

  return (
    <div>
      <h2>Weather Display</h2>
      
      <div style={{ marginBottom: '20px', display: 'flex', gap: '10px', flexWrap: 'wrap' }}>
        {weatherTypes.map(w => (
          <button
            key={w}
            onClick={() => setWeather(w)}
            style={{
              padding: '10px 20px',
              background: weather === w ? '#2196f3' : '#e0e0e0',
              color: weather === w ? 'white' : '#333',
              border: 'none',
              borderRadius: '5px',
              textTransform: 'capitalize'
            }}
          >
            {w}
          </button>
        ))}
      </div>

      <div style={{
        padding: '40px',
        background: 'white',
        borderRadius: '8px',
        textAlign: 'center',
        fontSize: '80px'
      }}>
        {weather === "sunny" && "β˜€οΈ"}
        {weather === "cloudy" && "☁️"}
        {weather === "rainy" && "🌧️"}
        {weather === "stormy" && "β›ˆοΈ"}
        {weather === "snowy" && "❄️"}
        {weather === "windy" && "πŸ’¨"}
      </div>

      <div style={{ marginTop: '15px', textAlign: 'center', fontSize: '20px', fontWeight: 'bold' }}>
        {weather === "sunny" && "It's a beautiful day!"}
        {weather === "cloudy" && "Cloudy skies today"}
        {weather === "rainy" && "Don't forget your umbrella!"}
        {weather === "stormy" && "Stay inside if possible!"}
        {weather === "snowy" && "Perfect for hot chocolate!"}
        {weather === "windy" && "Hold onto your hat!"}
      </div>
    </div>
  );
}

// ==========================================
// DEMO 4: Comparison with Nested Ternary
// ==========================================

function ComparisonDemo() {
  const [level, setLevel] = useState('beginner');

  return (
    <div>
      <h2>Nested Ternary vs Multiple &&</h2>
      
      <div style={{ marginBottom: '20px', display: 'flex', gap: '10px' }}>
        {['beginner', 'intermediate', 'advanced'].map(l => (
          <button
            key={l}
            onClick={() => setLevel(l)}
            style={{
              padding: '10px 20px',
              background: level === l ? '#264653' : '#e0e0e0',
              color: level === l ? 'white' : '#333',
              border: 'none',
              borderRadius: '5px',
              textTransform: 'capitalize'
            }}
          >
            {l}
          </button>
        ))}
      </div>

      <div style={{ display: 'grid', gap: '20px', gridTemplateColumns: '1fr 1fr' }}>
        {/* Nested Ternary (Messy) */}
        <div style={{ padding: '20px', background: '#ffebee', borderRadius: '8px' }}>
          <h3 style={{ color: '#c62828', marginTop: 0 }}>❌ Nested Ternary</h3>
          <pre style={{ background: '#1e1e1e', color: '#d4d4d4', padding: '15px', borderRadius: '4px', fontSize: '12px', overflow: 'auto' }}>
{`{level === "beginner"
  ? "πŸ‘Ά"
  : level === "intermediate"
    ? "πŸ’ͺ"
    : "πŸ”₯"
}`}
          </pre>
          <div style={{ marginTop: '10px', padding: '15px', background: 'white', borderRadius: '4px', textAlign: 'center', fontSize: '40px' }}>
            {level === "beginner" ? "πŸ‘Ά" : level === "intermediate" ? "πŸ’ͺ" : "πŸ”₯"}
          </div>
          <p style={{ fontSize: '13px', color: '#666', marginTop: '10px' }}>
            <em>Hard to read, easy to break nesting</em>
          </p>
        </div>

        {/* Multiple && (Clean) */}
        <div style={{ padding: '20px', background: '#e8f5e9', borderRadius: '8px' }}>
          <h3 style={{ color: '#2e7d32', marginTop: 0 }}>βœ… Multiple &&</h3>
          <pre style={{ background: '#1e1e1e', color: '#d4d4d4', padding: '15px', borderRadius: '4px', fontSize: '12px', overflow: 'auto' }}>
{`{level === "beginner" && "πŸ‘Ά"}
{level === "intermediate" && "πŸ’ͺ"}
{level === "advanced" && "πŸ”₯"}`}
          </pre>
          <div style={{ marginTop: '10px', padding: '15px', background: 'white', borderRadius: '4px', textAlign: 'center', fontSize: '40px' }}>
            {level === "beginner" && "πŸ‘Ά"}
            {level === "intermediate" && "πŸ’ͺ"}
            {level === "advanced" && "πŸ”₯"}
          </div>
          <p style={{ fontSize: '13px', color: '#666', marginTop: '10px' }}>
            <em>Flat, readable, easy to extend!</em>
          </p>
        </div>
      </div>

      <div style={{ marginTop: '20px', padding: '15px', background: '#fff3e0', borderRadius: '8px' }}>
        <h4>πŸ’‘ Why && is Better for 3+ Options:</h4>
        <ul style={{ margin: '10px 0 0 0' }}>
          <li>Each condition is independent</li>
          <li>No nesting = no confusion</li>
          <li>Add new option = just add one line</li>
          <li>Reads like a checklist</li>
        </ul>
      </div>
    </div>
  );
}

export default MultiConditionalRenderingMasterClass;

🧠 Memory Aids for Poor Logic Thinking

The "Light Switch" Analogy

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  && OPERATOR = LIGHT SWITCHES IN A ROW            β”‚
β”‚                                                 β”‚
β”‚  You have 3 light switches, only ONE can be ON   β”‚
β”‚                                                 β”‚
β”‚  Switch 1: level === "beginner"                   β”‚
β”‚  Switch 2: level === "intermediate"               β”‚
β”‚  Switch 3: level === "advanced"                   β”‚
β”‚                                                 β”‚
β”‚  SCENARIO: level = "intermediate"                 β”‚
β”‚                                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚  β”‚ Switch 1β”‚  β”‚ Switch 2β”‚  β”‚ Switch 3β”‚         β”‚
β”‚  β”‚ beginnerβ”‚  β”‚intermediateβ”‚  β”‚ advancedβ”‚       β”‚
β”‚  β”‚   OFF   β”‚  β”‚   ON    β”‚  β”‚   OFF   β”‚         β”‚
β”‚  β”‚  false  β”‚  β”‚  true   β”‚  β”‚  false  β”‚         β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β”‚       ↓            ↓            ↓               β”‚
β”‚  false && "πŸ‘Ά"  true && "πŸ’ͺ"  false && "πŸ”₯"    β”‚
β”‚       ↓            ↓            ↓               β”‚
β”‚   NOTHING      "πŸ’ͺ" RENDERS   NOTHING          β”‚
β”‚                                                 β”‚
β”‚  RESULT: Only "πŸ’ͺ" shows up!                    β”‚
β”‚                                                 β”‚
β”‚  WHY? Because of SHORT CIRCUITING:              β”‚
β”‚  β€’ false && anything = false (stops immediately)β”‚
β”‚  β€’ true && "πŸ’ͺ" = "πŸ’ͺ" (continues and returns)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The "Vending Machine" Analogy

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  && OPERATOR = VENDING MACHINE BUTTONS            β”‚
β”‚                                                 β”‚
β”‚  You want a snack. You press ONE button.         β”‚
β”‚  The machine checks each button:                 β”‚
β”‚                                                 β”‚
β”‚  Button A (beginner):                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Did they press A?                      β”‚    β”‚
β”‚  β”‚  NO β†’ Skip, give nothing               β”‚    β”‚
β”‚  β”‚  (false && "πŸ‘Ά" β†’ false)               β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                      ↓                          β”‚
β”‚  Button B (intermediate):                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Did they press B?                      β”‚    β”‚
β”‚  β”‚  YES β†’ Give "πŸ’ͺ" snack!                β”‚    β”‚
β”‚  β”‚  (true && "πŸ’ͺ" β†’ "πŸ’ͺ")                 β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                      ↓                          β”‚
β”‚  Button C (advanced):                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Did they press C?                      β”‚    β”‚
β”‚  β”‚  NO β†’ Skip, give nothing               β”‚    β”‚
β”‚  β”‚  (false && "πŸ”₯" β†’ false)               β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                                 β”‚
β”‚  CUSTOMER GETS: "πŸ’ͺ" (only one snack!)          β”‚
β”‚                                                 β”‚
β”‚  KEY RULE: Only ONE button can be pressed!       β”‚
β”‚  (Only ONE condition can be true!)               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Short Circuiting Visual

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  SHORT CIRCUITING EXPLAINED                       β”‚
β”‚                                                 β”‚
β”‚  && OPERATOR STOPS AT FIRST FALSE:               β”‚
β”‚                                                 β”‚
β”‚  true && "A" β†’ "A"                               β”‚
β”‚  false && "A" β†’ false (STOPS! "A" never checked) β”‚
β”‚                                                 β”‚
β”‚  In our skill example:                           β”‚
β”‚                                                 β”‚
β”‚  level = "intermediate"                          β”‚
β”‚                                                 β”‚
β”‚  Check 1: level === "beginner"                   β”‚
β”‚  "intermediate" === "beginner" β†’ false           β”‚
β”‚  false && "πŸ‘Ά" β†’ false βœ— STOP!                   β”‚
β”‚                                                 β”‚
β”‚  Check 2: level === "intermediate"               β”‚
β”‚  "intermediate" === "intermediate" β†’ true        β”‚
β”‚  true && "πŸ’ͺ" β†’ "πŸ’ͺ" βœ“ RENDER!                   β”‚
β”‚                                                 β”‚
β”‚  Check 3: level === "advanced"                   β”‚
β”‚  "intermediate" === "advanced" β†’ false           β”‚
β”‚  false && "πŸ”₯" β†’ false βœ— STOP!                   β”‚
β”‚                                                 β”‚
β”‚  Result: Only "πŸ’ͺ" is rendered!                  β”‚
β”‚                                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  REACT IGNORES: false, null, undefined β”‚    β”‚
β”‚  β”‚  So false doesn't show up in the DOM!  β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Comparison Table

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  WHEN TO USE WHICH METHOD?                        β”‚
β”‚                                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ METHOD          β”‚ USE WHEN...             β”‚  β”‚
β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚
β”‚  β”‚ && (AND)        β”‚ 2 options: show/hide    β”‚  β”‚
β”‚  β”‚                 β”‚ {show && <Component />} β”‚  β”‚
β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚
β”‚  β”‚ ?: (Ternary)    β”‚ 2 options: A or B       β”‚  β”‚
β”‚  β”‚                 β”‚ {x ? "A" : "B"}         β”‚  β”‚
β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚
β”‚  β”‚ Multiple &&     β”‚ 3+ options: A or B or C β”‚  β”‚
β”‚  β”‚                 β”‚ {a && "A"}              β”‚  β”‚
β”‚  β”‚                 β”‚ {b && "B"}              β”‚  β”‚
β”‚  β”‚                 β”‚ {c && "C"}              β”‚  β”‚
β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚
β”‚  β”‚ Nested Ternary  β”‚ 3+ options (AVOID!)     β”‚  β”‚
β”‚  β”‚                 β”‚ Hard to read            β”‚  β”‚
β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚
β”‚  β”‚ if/else         β”‚ Completely different UI β”‚  β”‚
β”‚  β”‚ (Early Return)  β”‚ Guard clauses           β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                 β”‚
β”‚  DECISION TREE:                                 β”‚
β”‚                                                 β”‚
β”‚  How many options?                               β”‚
β”‚     2 β†’ Use && or Ternary                        β”‚
β”‚     3+ β†’ Use Multiple && (preferred!)            β”‚
β”‚     Completely different β†’ Use Early Return      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸŽ“ Practice Exercises

Exercise 1: Create a Grade Display

Create a component that shows different emojis for grades:

// Grades: A, B, C, D, F
// A = πŸ†, B = πŸ‘, C = πŸ‘Œ, D = ⚠️, F = ❌

function GradeBadge({ grade }) {
  // TODO: Use multiple && to show correct emoji
  return <span>{/* your code here */}</span>;
}

Solution:

function GradeBadge({ grade }) {
  return (
    <span className="grade-badge">
      {grade === "A" && "πŸ†"}
      {grade === "B" && "πŸ‘"}
      {grade === "C" && "πŸ‘Œ"}
      {grade === "D" && "⚠️"}
      {grade === "F" && "❌"}
      <span>{grade}</span>
    </span>
  );
}

Exercise 2: Fix the Bug

This code has a bug. The emojis don't show correctly:

function Skill({ level }) {
  return (
    <div>
      {level === "beginner" && "πŸ‘Ά"}
      {level == "intermediate" && "πŸ’ͺ"}
      {level === "advanced" && "πŸ”₯"}
    </div>
  );
}

Bug: Line 4 uses == (loose equality) instead of === (strict equality). While this might work, it's inconsistent and can cause bugs.

Solution:

function Skill({ level }) {
  return (
    <div>
      {level === "beginner" && "πŸ‘Ά"}
      {level === "intermediate" && "πŸ’ͺ"}  // Fixed: use ===
      {level === "advanced" && "πŸ”₯"}
    </div>
  );
}

Exercise 3: Create Priority Badge

Create a priority badge with 4 levels:

// Priority: low, medium, high, critical
// low = πŸ”΅ Low, medium = 🟑 Medium, high = 🟠 High, critical = πŸ”΄ Critical

function PriorityBadge({ priority }) {
  // TODO: Create with multiple && operators
}

Solution:

function PriorityBadge({ priority }) {
  return (
    <span className={`badge badge-${priority}`}>
      {priority === "low" && "πŸ”΅"}
      {priority === "medium" && "🟑"}
      {priority === "high" && "🟠"}
      {priority === "critical" && "πŸ”΄"}
      <span style={{ marginLeft: '5px', textTransform: 'capitalize' }}>
        {priority}
      </span>
    </span>
  );
}

πŸ’‘ Key Takeaways

ConceptWhat It MeansExample
Multiple &&Check multiple conditions, render matching one{a && "A"} {b && "B"} {c && "C"}
Mutually ExclusiveOnly ONE condition can be truelevel can't be both "beginner" AND "advanced"
Short Circuiting&& stops at first falsefalse && anything β†’ false
vs TernaryUse && for 3+, ternary for 22 options: ? :, 3+ options: &&
ReadabilityFlat structure is easier to readNo nesting!

The Multi-Option Pattern:

// For 3+ options, use multiple &&:
{condition1 && "Result A"}
{condition2 && "Result B"}
{condition3 && "Result C"}

// Only ONE will render!

Golden Rules:

  1. 3+ options? Use multiple && operators (NOT nested ternaries!)
  2. Mutually exclusive - only one condition should be true
  3. Order doesn't matter if conditions are truly exclusive
  4. Easy to extend - just add another line
  5. React ignores false - so non-matching conditions show nothing

One Sentence Summary: > "When you have 3 or more options (like beginner/intermediate/advanced), use multiple && operators on separate lines instead of nested ternaries - each line checks one condition and only the matching one renders, while the others safely disappear thanks to short-circuiting!"