Bird
Raised Fist0
Reactframework~10 mins

If conditions in JSX in React - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - If conditions in JSX
Start Render
↓
Evaluate Condition
↓
Render JSX
↓
Update DOM
React checks the condition during render. If true, it shows one JSX part; if false, it shows another or nothing.
Execution Sample
React
function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <p>Welcome back!</p> : <p>Please sign in.</p>}
    </div>
  );
}
This component shows a welcome message if logged in, else asks to sign in.
Execution Table
StepisLoggedInCondition (isLoggedIn ?)Rendered JSXDOM Update
1truetrue<p>Welcome back!</p>Shows 'Welcome back!' message
2falsefalse<p>Please sign in.</p>Shows 'Please sign in.' message
3nullfalse<p>Please sign in.</p>Shows 'Please sign in.' message
4undefinedfalse<p>Please sign in.</p>Shows 'Please sign in.' message
50false<p>Please sign in.</p>Shows 'Please sign in.' message
6''false<p>Please sign in.</p>Shows 'Please sign in.' message
💡 Rendering ends after JSX is chosen based on condition.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6
isLoggedInundefinedtruefalsenullundefined0''
Key Moments - 2 Insights
Why does the component show 'Please sign in.' when isLoggedIn is null or undefined?
Because in the execution_table rows 3 and 4, the condition evaluates to false for null and undefined, so the else JSX is rendered.
What happens if isLoggedIn is 0 or an empty string?
As shown in rows 5 and 6, 0 and '' are treated as false in the condition, so the else JSX is shown.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what JSX is rendered when isLoggedIn is true?
A<p>Please sign in.</p>
BNothing is rendered
C<p>Welcome back!</p>
DAn error occurs
💡 Hint
Check row 1 in the execution_table where isLoggedIn is true.
At which step does the condition become false for the first time?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the Condition column in execution_table rows.
If isLoggedIn was changed to a non-empty string, how would the rendered JSX change?
AIt would render <p>Welcome back!</p>
BIt would render <p>Please sign in.</p>
CIt would render nothing
DIt would cause an error
💡 Hint
Non-empty strings are truthy in JavaScript, so condition becomes true.
Concept Snapshot
JSX if conditions use JavaScript expressions inside braces.
Use ternary: condition ? JSX if true : JSX if false.
Falsy values (false, 0, '', null, undefined) render the else part.
Truthy values render the true part.
This controls what React shows on screen.
Full Transcript
In React JSX, you can use if conditions by writing JavaScript expressions inside curly braces. The most common way is using the ternary operator: condition ? true JSX : false JSX. React evaluates the condition during rendering. If the condition is true, it shows the first JSX; if false, it shows the second. For example, if isLoggedIn is true, it shows 'Welcome back!'. If false or other falsy values like null or empty string, it shows 'Please sign in.'. This lets you control what the user sees based on state or props. Remember, JavaScript truthy and falsy rules apply here.

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

  1. Step 1: Understand JSX conditional rendering with &&

    In JSX, condition && expression renders the expression only if the condition is true.
  2. 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.
  3. Final Answer:

    {isLoggedIn && <p>Welcome back!</p>} -> Option B
  4. 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

  1. Step 1: Understand ternary syntax in JSX

    The ternary operator syntax is condition ? expressionIfTrue : expressionIfFalse.
  2. 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.
  3. Final Answer:

    {showHello ? <p>Hello</p> : <p>Goodbye</p>} -> Option D
  4. Quick Check:

    Ternary = condition ? true : false [OK]
Hint: Ternary: condition ? true : false for two choices [OK]
Common Mistakes:
  • Using if/else statements directly inside JSX
  • Swapping true and false expressions
  • Using && when two choices are needed
3. What will be rendered by this React component?
function Greeting({ isMorning }) {
  return (
    <div>
      {isMorning ? <h1>Good morning!</h1> : <h1>Good evening!</h1>}
      {isMorning && <p>Have a nice day!</p>}
    </div>
  );
}

If isMorning is false?
medium
A.

Good evening!

B.

Good evening!

Have a nice day!

C.

Good morning!

Have a nice day!

D.

Good morning!

Solution

  1. Step 1: Evaluate the ternary expression with isMorning = false

    The ternary isMorning ? <h1>Good morning!</h1> : <h1>Good evening!</h1> renders <h1>Good evening!</h1> because isMorning is false.
  2. Step 2: Evaluate the && expression with isMorning = false

    The expression isMorning && <p>Have a nice day!</p> renders nothing because isMorning is false.
  3. Final Answer:

    <div><h1>Good evening!</h1></div> -> Option A
  4. Quick Check:

    False && anything = nothing [OK]
Hint: False && expression renders nothing; ternary picks false branch [OK]
Common Mistakes:
  • Assuming && renders content when condition is false
  • Mixing up ternary true/false branches
  • Expecting both expressions to render always
4. Identify the error in this JSX snippet:
return (
  <div>
    {isActive ? <p>Active</p>}
  </div>
);
medium
A. No error, code is valid
B. Using if statement inside JSX
C. Missing else part in ternary operator
D. Incorrect use of && operator

Solution

  1. Step 1: Check ternary operator syntax

    The ternary operator requires both true and false expressions: condition ? exprIfTrue : exprIfFalse. Here, the false part is missing.
  2. Step 2: Understand JSX requirements

    Without the false expression, JSX will throw a syntax error because the ternary is incomplete.
  3. Final Answer:

    Missing else part in ternary operator -> Option C
  4. 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>)}

Solution

  1. Step 1: Understand conditional rendering inside map

    To render only active users, use a ternary that returns the list item or null.
  2. Step 2: Check key usage and syntax

    {users.map(user => user.isActive ? <li key={user.id}>{user.name}</li> : null)} correctly uses key={user.id} and returns null for inactive users, which React ignores.
  3. Step 3: Analyze other options

    {users.map(user => if (user.isActive) return <li key={user.id}>{user.name}</li> )} uses invalid syntax: `if` statement directly after arrow function requires block `{}` braces. {users.map(user => user.isActive || <li>{user.name}</li>)} uses `||` which renders items for inactive users and misses keys. {users.map(user => user.isActive ? <li>{user.name}</li> : <li>Inactive</li>)} shows inactive users with text, which is not desired.
  4. Final Answer:

    {users.map(user => user.isActive ? <li key={user.id}>{user.name}</li> : null)} -> Option A
  5. Quick Check:

    Use ternary with key and null for conditional list rendering [OK]
Hint: Use ternary with key and null to skip items [OK]
Common Mistakes:
  • Missing key prop in list items
  • Using if statements inside map without return
  • Rendering false or undefined instead of null
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard