Expressions in JSX
Expressions in JSX let you put live JavaScript values inside your markup by wrapping them in curly braces. Anything that produces a value, a variable, a calculation, a function call or a ternary, can go between { and }, and React will render the result. This is how components show names, totals, dates and anything else that changes.
What curly braces accept
The rule is simple: braces take an expression, meaning code that evaluates to a value. They do not take statements such as if, for or variable declarations.
function OrderSummary({ customer, items, discount }) {
const subtotal = items.reduce((sum, item) => sum + item.price * item.qty, 0);
const total = subtotal - discount;
return (
<section className={total > 1000 ? 'summary big-order' : 'summary'}>
<h2>Hello, {customer.firstName}!</h2>
<p>You have {items.length} {items.length === 1 ? 'item' : 'items'} in your cart.</p>
<p>Subtotal: Rs {subtotal.toFixed(2)}</p>
{discount > 0 && <p>Discount: Rs {discount}</p>}
<p><strong>Total: Rs {total.toFixed(2)}</strong></p>
<p>Ordered on {new Date().toLocaleDateString('en-IN')}</p>
<ul>
{items.map((item) => (
<li key={item.id}>{item.name.toUpperCase()} x {item.qty}</li>
))}
</ul>
</section>
);
}
This one component uses property access, arithmetic, method calls, a ternary for plural words, the && operator for an optional line, and map to turn an array into list items.
Expressions in attributes
Braces also work for attribute values. className={...} picks a class at runtime, src={user.avatarUrl} sets an image path, and disabled={!isValid} passes a real boolean. When an attribute takes a fixed string, use quotes instead: type="email". Do not wrap braces in quotes; src="{url}" sends the literal text {url}.
The style attribute takes an object, which is why it has double braces: style={{ color: 'red', marginTop: 8 }}. The outer pair opens an expression and the inner pair is the object.
How different values render
| Value inside braces | What appears |
|---|---|
| String or number | The text, e.g. 42 |
| Array of elements | Each element in order |
true, false, null, undefined | Nothing at all |
| A plain object | An error: objects are not valid as a React child |
Where people get caught out
The best-known trap is {count && <Badge />} when count is 0. Because 0 is falsy, && returns 0 itself, and React renders a stray 0 on the page, since numbers are displayed. Write {count > 0 && <Badge />} so the left side is a true boolean.
The second common error is trying to render an object, such as {user} or a Date object. Pick a field like {user.name}, or convert with a method like toLocaleDateString().
Third, people try to write if inside JSX. It is a statement, so it is not allowed. Use a ternary, &&, or compute the value in a variable above the return.
Keeping JSX readable
Long expressions make markup hard to scan. If a calculation spans more than a short line, move it above the return into a named variable, as subtotal and total are above. Nested ternaries are a sign you should extract a small helper function or a separate component.
In a nutshell
Curly braces drop any JavaScript expression into JSX, in content and in attributes. Strings and numbers render as text, booleans and null render nothing, objects cause an error. Guard && against zero, and keep heavy logic outside the markup.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.