React JSX Expressions
⚛️ React JSX Expressions
JSX expressions allow you to embed JavaScript logic directly inside JSX.
Anything inside {} in JSX is treated as a JavaScript expression.
Basic Syntax
{name}is a JSX expressionIt evaluates to the value of
nameand renders it
Expressions Can Be Any JavaScript Expression
Examples:
Expressions in Attributes
You can use expressions in JSX attributes:
Calling Functions in JSX
You can call JavaScript functions inside {}:
Conditional Rendering in Expressions
Using ternary:
Using logical AND:
Expressions in Loops (map())
❌ What You Cannot Do in JSX Expressions
Cannot use statements, only expressions
❌if (true) { ... }→ invalid
✔ Use ternary or logical operators insteadCannot use
forloops inside{}
✔ Use.map()instead
🎯 Summary
| Feature | Example |
|---|---|
| Embed variable | {name} |
| Arithmetic | {a + b} |
| Conditional | {isLoggedIn ? "Yes" : "No"} |
| Function call | {formatName(user)} |
| Array mapping | {numbers.map(n => <li>{n}</li>)} |
| Attribute expressions | <div style={{ color: "red" }}>Text</div> |
JSX expressions make React dynamic and interactive, letting you combine JavaScript logic and UI seamlessly.
