How do you write a simple if condition inside JSX to show a message only if a variable isLoggedIn is true?
You can use the logical AND operator: {isLoggedIn && <p>Welcome back!</p>}. This shows the message only when isLoggedIn is true.
Click to reveal answer
beginner
What is the purpose of the ternary operator in JSX?
The ternary operator condition ? expr1 : expr2 lets you choose between two things to show in JSX depending on a condition. For example: {isLoggedIn ? <LogoutButton /> : <LoginButton />}.
Click to reveal answer
beginner
Why can't you use a regular if statement directly inside JSX?
JSX expects expressions, but a regular if statement is a statement, not an expression. So you use expressions like ternary operators or logical AND inside JSX instead.
Click to reveal answer
intermediate
How can you conditionally render multiple elements in JSX?
You can wrap multiple elements in a fragment <> ... </> and use a ternary operator or logical AND outside it. For example: {isLoggedIn ? <><Welcome /><LogoutButton /></> : <LoginButton />}.
Click to reveal answer
intermediate
What is a clean way to handle complex if conditions in JSX?
You can move the if logic outside JSX into variables or functions, then use those variables inside JSX. This keeps JSX clean and easy to read.
Click to reveal answer
Which of these is a valid way to conditionally render a message in JSX when showMessage is true?
A{showMessage && <p>Hello!</p>}
Bif(showMessage) { <p>Hello!</p> }
C{if(showMessage) <p>Hello!</p>}
D<p>{if(showMessage) Hello!}</p>
✗ Incorrect
Only option A uses a valid JSX expression with logical AND. Regular if statements can't be used directly inside JSX.
What does this JSX expression do? {isAdmin ? <AdminPanel /> : <UserPanel />}
AAlways shows both AdminPanel and UserPanel
BShows AdminPanel if isAdmin is true, else UserPanel
CShows nothing
DThrows an error
✗ Incorrect
The ternary operator chooses one component to show based on the condition.
Why might you move if conditions outside JSX?
ATo make the code run faster
BBecause JSX does not allow any conditions
CTo avoid using React hooks
DTo keep JSX clean and easier to read
✗ Incorrect
Moving logic outside JSX helps keep the JSX simple and readable.
Which operator is commonly used for simple conditional rendering in JSX?
A++ (increment)
B|| (logical OR)
C&& (logical AND)
D?? (nullish coalescing)
✗ Incorrect
Logical AND is often used to render something only if a condition is true.
What will this render if isLoggedIn is false? {isLoggedIn && <p>Welcome!</p>}
ANothing (renders null)
BThe paragraph with Welcome!
CAn error
DUndefined
✗ Incorrect
If the condition is false, logical AND returns false which React treats as nothing to render.
Explain how to use if conditions inside JSX to show different content based on a variable.
Think about expressions vs statements in JSX.
You got /4 concepts.
Describe best practices for handling complex conditional rendering in React JSX.
Consider code readability and maintainability.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is the correct way to conditionally render a message in JSX when isLoggedIn is true?
easy
A. {if (isLoggedIn) <p>Welcome back!</p>}
B. {isLoggedIn && <p>Welcome back!</p>}
C. {isLoggedIn ? <p>Welcome back!</p>}
D. {isLoggedIn || <p>Welcome back!</p>}
Solution
Step 1: Understand JSX conditional rendering with &&
In JSX, condition && expression renders the expression only if the condition is true.
Step 2: Check each option's syntax
{isLoggedIn && <p>Welcome back!</p>} uses {isLoggedIn && <p>Welcome back!</p>}, which is correct. {if (isLoggedIn) <p>Welcome back!</p>} uses an invalid if statement inside JSX. {isLoggedIn ? <p>Welcome back!</p>} uses an incomplete ternary operator without the false branch (`: expression`), causing a syntax error. {isLoggedIn || <p>Welcome back!</p>} uses ||, which is incorrect for this purpose.
Final Answer:
{isLoggedIn && <p>Welcome back!</p>} -> Option B
Quick Check:
Use && for simple true condition rendering [OK]
Hint: Use && to show content only if true [OK]
Common Mistakes:
Trying to use if statements directly inside JSX
Using || instead of && for conditional rendering
Confusing ternary with && when only one condition is needed
2. Which JSX snippet correctly uses a ternary operator to show <p>Hello</p> if showHello is true, otherwise <p>Goodbye</p>?
easy
A. {showHello && <p>Hello</p> else <p>Goodbye</p>}
B. {showHello ? <p>Goodbye</p> : <p>Hello</p>}
C. {if (showHello) <p>Hello</p> else <p>Goodbye</p>}
D. {showHello ? <p>Hello</p> : <p>Goodbye</p>}
Solution
Step 1: Understand ternary syntax in JSX
The ternary operator syntax is condition ? expressionIfTrue : expressionIfFalse.
Step 2: Match the expressions to the condition
{showHello ? <p>Hello</p> : <p>Goodbye</p>} correctly shows Hello if showHello is true, else Goodbye. {showHello && <p>Hello</p> else <p>Goodbye</p>} uses invalid syntax with else inside JSX. {if (showHello) <p>Hello</p> else <p>Goodbye</p>} tries to use if statement inside JSX, which is invalid. {showHello ? <p>Goodbye</p> : <p>Hello</p>} reverses the messages.
Final Answer:
{showHello ? <p>Hello</p> : <p>Goodbye</p>} -> Option D
Quick Check:
Ternary = condition ? true : false [OK]
Hint: Ternary: condition ? true : false for two choices [OK]
The ternary operator requires both true and false expressions: condition ? exprIfTrue : exprIfFalse. Here, the false part is missing.
Step 2: Understand JSX requirements
Without the false expression, JSX will throw a syntax error because the ternary is incomplete.
Final Answer:
Missing else part in ternary operator -> Option C
Quick Check:
Ternary needs both ? and : parts [OK]
Hint: Ternary must have both ? and : parts [OK]
Common Mistakes:
Omitting the else (false) part in ternary
Confusing ternary with && operator
Trying to use if statements inside JSX
5. You want to render a list of user names, but only show the name if the user is active. Which JSX code correctly implements this using map and conditional rendering?
hard
A. {users.map(user => user.isActive ? <li key={user.id}>{user.name}</li> : null)}
B. {users.map(user => if (user.isActive) return <li key={user.id}>{user.name}</li> )}
C. {users.map(user => user.isActive || <li>{user.name}</li>)}
D. {users.map(user => user.isActive ? <li>{user.name}</li> : <li>Inactive</li>)}