Interview Q&A / React

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How do you use JavaScript expressions inside JSX, and what cannot go inside the braces?

SHORT ANSWER

Curly braces accept any JavaScript expression: a variable, a function call, a ternary. Statements such as if, for or switch are not expressions, so they must live outside the JSX.

Explain it simply

Curly braces are a window from the markup into JavaScript. Whatever sits in the window must produce a value, because React puts that value on the screen.

{price * 2}, {user.name} and {isOpen ? "Open" : "Closed"} all produce values. An if block does not produce a value, so it cannot go in the braces. Do the if before the return, or use a ternary or &&.

Example

jsx
function Fare({ km, isStudent }) {
  const base = km * 2;
  return (
    <p>
      Fare: Rs {isStudent ? base / 2 : base}
      {km > 500 && <span> (long route)</span>}
    </p>
  );
}
// Fare km=100 isStudent=true renders: Fare: Rs 100

Objects work too, but note the double braces in style={{ color: "red" }}: the outer pair opens JavaScript, the inner pair is the object.

What to say in the interview

"Braces in JSX take any expression, such as variables, calls, ternaries and logical AND. Statements like if or for are not allowed, so I compute the value above the return or use a ternary. The double braces in style are just an object literal inside the expression braces."

Common mistakes

  • Writing if (...) { } directly inside the braces.
  • Rendering a plain object as a child, which throws an error.

#jsx#expressions

Related questions

Practise more React questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open React quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.