Embedding JavaScript Expressions in JSX0%

Embedding JavaScript Expressions in JSX

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Embedding JavaScript Expressions in JSX

1. The Curly Braces {} Window

JSX allows you to seamlessly integrate dynamic JavaScript logic directly into your presentation markup. Whenever you open curly braces {} within JSX, you are opening a window back into JavaScript.

Inside these curly braces, you can write any valid JavaScript expression that produces a value.

JSX
export default function Greeting() {
const studentName = 'Priya Sharma';
const score = 94;
 
return (
<div className="profile-banner">
<h1>Hello, {studentName}!</h1>
<p>Your assessment score is: {score} / 100</p>
<p>Status: {score >= 80 ? 'Distinction' : 'Standard Pass'}</p>
</div>
);
}

2. Expressions vs Statements in JSX

A critical concept that trips up beginners is the distinction between a JavaScript expression and a JavaScript statement:

  • Expressions (Allowed in JSX): An expression evaluates to a single value. Examples include variable names, arithmetic operations, function calls, template literals, and ternary operators.
  • Statements (Forbidden in JSX): Statements perform actions or declare control flow but do not evaluate to a single value. Examples include if/else blocks, for loops, while loops, and variable declarations (const x = 10;).
JSX
// ❌ Syntax Error: 'if' is a statement, not an expression
<p>{if (isLoggedIn) { 'Welcome!' }}</p>
 
// ✅ Valid: Ternary operator is an expression that evaluates to a value
<p>{isLoggedIn ? 'Welcome back!' : 'Please sign in'}</p>
 
// ✅ Valid: Function invocation is an expression
<p>Today is: {new Date().toLocaleDateString()}</p>
 
// ✅ Valid: Array mapping produces a new array of elements
<ul>
{['React', 'Vite', 'TypeScript'].map(item => <li key={item}>{item}</li>)}
</ul>

3. Passing Dynamic Attributes via {}

Curly braces are not only for element text content; they are also used to bind dynamic attributes and properties:

JSX
const user = {
name: 'Rahul Verma',
avatarUrl: 'https://images.unsplash.com/photo-1534528741775-53994a69daeb',
imageSize: 90
};
 
export default function Avatar() {
return (
<img
className="avatar-circle"
src={user.avatarUrl}
alt={`Profile photo of ${user.name}`}
width={user.imageSize}
height={user.imageSize}
/>
);
}

Note: Do not wrap curly braces in quotes when passing dynamic variables (src={user.avatarUrl}, NOT src="{user.avatarUrl}"). Quotes treat the value as a literal string.

4. How JSX Renders Different JavaScript Types

When rendering values inside {}:

  • Strings and Numbers: Rendered directly to the screen as text nodes ({'MSK'} -> MSK, {42} -> 42).
  • Booleans (true, false), null, and undefined: Rendered as nothing (empty). They produce no DOM output, making them ideal for conditional rendering.
  • Arrays: Flattened and each item is rendered sequentially ({[<span key="1">A</span>, <span key="2">B</span>]}).
  • Objects: Cannot be rendered directly as children! Attempting to render an object ({user}) throws an error: "Objects are not valid as a React child".
JSX
// ❌ Throws runtime error: Cannot render object directly
const user = { name: 'Aman' };
return <div>{user}</div>;
 
// ✅ Valid: Access specific string/primitive properties
return <div>{user.name}</div>;

Interactive Quiz

Practice Quiz

Question 1
What syntax is used to embed dynamic JavaScript expressions inside JSX?
Question 2
Why can you NOT use a standard if/else block directly inside JSX {}?
Question 3
What happens when React encounters null, undefined, or false inside {} in JSX?
Question 4
Which of the following is an INCORRECT way to bind a dynamic image source in JSX?
Question 5
What error occurs if you try to render a plain JavaScript object directly as a child in JSX (e.g. const item = { id: 1 }; return <div>{item}</div>;)?
Next Lesson

JSX Attributes, ClassName, and Self-Closing Tags

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext