Course contentsShow
← React
Lesson 90 of 2,3963. JSX FundamentalsFree lesson

What You Can't Interpolate: Statements

Understanding why if/else, for loops, and other statements don't work inside curly braces.

What You Can't Interpolate: Statements

You'll learn: Why if, for, and other JavaScript statements don't work inside JSX curly braces.

The Rule: Expressions Only

In JSX, curly braces {} accept expressions (things that produce a value), not statements (instructions that perform actions). This is a hard rule of the syntax.

Think of it like a calculator display: you can show the result of math (an expression), but you can't show the act of doing homework (a statement). The display needs a final value.

What Doesn't Work

// ❌ Won't compile - if/else is a statement
<div>
  {if (isLoggedIn) { "Welcome" } else { "Please log in" }}
</div>

// ❌ Won't compile - for loop is a statement
<ul>
  {for (let i = 0; i < 3; i++) { <li>Item {i}</li> }}
</ul>

// ❌ Won't compile - variable declarations are statements
<div>
  {let greeting = "Hello"}
</div>

Why This Limitation Exists

JSX curly braces get compiled into function call arguments. JavaScript functions can only receive values (expressions), not raw statements. The transpiler literally can't turn if or for into something that fits inside React.createElement().

What You Can Do Instead

Use expressions that return values: ternary operators (? :), short-circuit evaluation (&&, ||), or array methods like .map() — all concepts you already know!

Key Takeaway: JSX curly braces only accept expressions that evaluate to a value. Control flow statements like if, for, and while must live outside the JSX or be replaced with expression-based alternatives.