Bird
Raised Fist0
Reactframework~20 mins

Ternary operator usage in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ternary Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What will this React component render?
Consider this React functional component using a ternary operator. What will be displayed when is rendered?
React
import React from 'react';

function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>}
    </div>
  );
}

export default Greeting;
A<div><h1>Please sign up.</h1></div>
B<div><h1>Welcome back!</h1></div>
C<div></div>
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check the value of the isLoggedIn prop and how the ternary operator chooses what to render.
❓ state_output
intermediate
2:00remaining
What is the output after clicking the button once?
This React component uses a ternary operator to display text based on state. What text will be shown after the button is clicked once?
React
import React, { useState } from 'react';

function ToggleText() {
  const [isOn, setIsOn] = useState(false);

  return (
    <div>
      <p>{isOn ? 'The switch is ON' : 'The switch is OFF'}</p>
      <button onClick={() => setIsOn(!isOn)}>Toggle</button>
    </div>
  );
}

export default ToggleText;
AThe switch is OFF
BNo text is displayed
CThe switch is ON
DTypeError: setIsOn is not a function
Attempts:
2 left
💡 Hint
The initial state is false. Clicking the button toggles it to true.
📝 Syntax
advanced
2:00remaining
Which option correctly uses a ternary operator inside JSX?
Select the option that correctly uses a ternary operator to conditionally render a message inside a React component.
Areturn <div>{isActive ? <p>Active</p> : <p>Inactive</p>}</div>;
Breturn <div>{if (isActive) {<p>Active</p>} else {<p>Inactive</p>}}</div>;
Creturn <div>{isActive && <p>Active</p> || <p>Inactive</p>}</div>;
Dreturn <div>{isActive ? <p>Active</p> else <p>Inactive</p>}</div>;
Attempts:
2 left
💡 Hint
Remember that JSX expressions must be valid JavaScript expressions. Ternary operators have a specific syntax.
🔧 Debug
advanced
2:00remaining
Why does this React component throw an error?
This component throws an error when rendered. What is the cause?
React
function Status({ status }) {
  return (
    <div>
      {status === 'loading' ? <p>Loading...</p> : status === 'error' ? <p>Error!</p> : <p>Done</p>}
    </div>
  );
}

export default Status;
ANested ternary operators without parentheses cause ambiguity and error
BThe component is missing a return statement
CThe component uses invalid JSX tags
DThe code runs correctly without errors
Attempts:
2 left
💡 Hint
Check if nested ternary operators are allowed in JSX without extra parentheses.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using ternary operators in React JSX?
Why do developers often prefer ternary operators over if-else statements inside JSX?
ATernary operators allow inline conditional rendering as expressions, which if-else statements cannot do inside JSX
BTernary operators execute faster than if-else statements in React components
CIf-else statements cause React to crash, but ternary operators do not
DTernary operators automatically memoize components, improving performance
Attempts:
2 left
💡 Hint
Think about what JSX expects inside curly braces and how expressions differ from statements.

Practice

(1/5)
1. What does the ternary operator condition ? expr1 : expr2 do in React JSX?
easy
A. It chooses expr1 if condition is true, otherwise expr2.
B. It always returns expr2 regardless of the condition.
C. It runs both expr1 and expr2 and returns an array.
D. It throws an error if condition is false.

Solution

  1. Step 1: Understand ternary operator structure

    The ternary operator has the form condition ? expr1 : expr2, which means it evaluates the condition first.
  2. Step 2: Evaluate the condition result

    If the condition is true, it returns expr1; if false, it returns expr2.
  3. Final Answer:

    It chooses expr1 if condition is true, otherwise expr2. -> Option A
  4. Quick Check:

    Ternary operator = choose between two values [OK]
Hint: Remember: condition ? trueValue : falseValue [OK]
Common Mistakes:
  • Thinking it runs both expressions
  • Confusing with logical AND/OR operators
  • Using it outside JSX incorrectly
2. Which of the following is the correct syntax to use a ternary operator inside JSX to display "Yes" if isActive is true, else "No"?
easy
A. {isActive ? "Yes"; "No"}
B. {isActive ? "Yes" : "No"}
C. {isActive ? "Yes"}
D. {isActive : "Yes" ? "No"}

Solution

  1. Step 1: Check ternary operator syntax in JSX

    The ternary operator must be inside curly braces in JSX and follow condition ? expr1 : expr2 format.
  2. Step 2: Validate each option

    {isActive ? "Yes" : "No"} correctly uses {isActive ? "Yes" : "No"}. Options B, C, and D have syntax errors or incomplete expressions.
  3. Final Answer:

    {isActive ? "Yes" : "No"} -> Option B
  4. Quick Check:

    Correct JSX ternary syntax = {isActive ? "Yes" : "No"} [OK]
Hint: Use curly braces and ? : inside JSX [OK]
Common Mistakes:
  • Missing colon ':' in ternary
  • Using semicolon instead of colon
  • Not wrapping expression in curly braces
3. What will this React component render?
function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
    </div>
  );
}

If isLoggedIn is false?
medium
A.

Please sign in.

B.

Welcome back!

C.
D. SyntaxError

Solution

  1. Step 1: Understand the ternary condition

    The component uses isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>. If isLoggedIn is false, it chooses the second expression.
  2. Step 2: Determine output when isLoggedIn is false

    Since isLoggedIn is false, it renders <h1>Please sign in.</h1> inside the div.
  3. Final Answer:

    <div><h1>Please sign in.</h1></div> -> Option A
  4. Quick Check:

    False condition shows second expression [OK]
Hint: False condition picks expression after colon ':' [OK]
Common Mistakes:
  • Choosing first expression for false condition
  • Confusing JSX tags rendering
  • Expecting empty div output
4. Identify the error in this JSX snippet:
return (
  <div>
    {isReady ? <p>Ready</p> : <p>Not ready</p>}
    {isReady ? <p>Go!</p> : null}
  </div>
);
medium
A. Ternary operator cannot be used inside JSX.
B. JSX elements must not be inside curly braces.
C. Second ternary is missing the else part after colon.
D. No error; code is valid.

Solution

  1. Step 1: Check each ternary usage

    The first ternary is correct with both true and false parts. The second uses {isReady ? <p>Go!</p> : null} which is valid syntax.
  2. Step 2: Understand ternary operator requirements

    The ternary operator requires both true and false expressions separated by a colon. Here, the else part is null, which is valid JSX.
  3. Final Answer:

    No error; code is valid. -> Option D
  4. Quick Check:

    Ternary needs both ? and : parts, and else can be null [OK]
Hint: Ternary always needs ? and : parts [OK]
Common Mistakes:
  • Omitting the else expression
  • Confusing ternary with logical AND
  • Thinking JSX can't have ternary
5. You want to display a list of user names with a label showing if they are online or offline using ternary inside JSX. Which code correctly implements this?
const users = [
  { name: "Alice", online: true },
  { name: "Bob", online: false }
];

function UserList() {
  return (
    <ul>
      {users.map(user => (
        <li key={user.name}>
          {user.name} - {user.online ? "Online" : "Offline"}
        </li>
      ))}
    </ul>
  );
}
hard
A. Does not render anything because key is missing.
B. Will throw an error because ternary cannot be used inside map.
C. Correctly shows each user with "Online" or "Offline" label.
D. Only shows "Online" for all users regardless of status.

Solution

  1. Step 1: Analyze the map and ternary usage

    The code maps over users and uses a ternary to show "Online" if user.online is true, else "Offline".
  2. Step 2: Check React key and JSX correctness

    Each <li> has a unique key from user.name, and the ternary is correctly inside JSX curly braces.
  3. Final Answer:

    Correctly shows each user with "Online" or "Offline" label. -> Option C
  4. Quick Check:

    Map + ternary inside JSX = correct rendering [OK]
Hint: Use map with ternary inside JSX for conditional labels [OK]
Common Mistakes:
  • Forgetting unique key in list items
  • Trying to use ternary outside JSX braces
  • Assuming ternary can't be used in map
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