Embedding expressions in JSX lets you show dynamic values inside your React components. It helps make your UI change based on data or user actions.
Embedding expressions in JSX in React
Introduction
Syntax
React
const element = <tag>{expression}</tag>;Use curly braces {} to embed any JavaScript expression inside JSX.
Expressions can be variables, math, function calls, or ternary conditions.
Examples
React
const name = 'Alex'; const greeting = <h1>Hello, {name}!</h1>;
React
const sum = 5 + 3; const result = <p>5 + 3 = {sum}</p>;
React
const isLoggedIn = true; const message = <p>{isLoggedIn ? 'Welcome back!' : 'Please sign in.'}</p>;
Sample Program
This React component shows a greeting that changes based on the time of day. It uses expressions inside JSX to embed variables and a condition.
React
import React from 'react'; function Greeting() { const userName = 'Jamie'; const hour = new Date().getHours(); const greetingMessage = hour < 12 ? 'Good morning' : 'Good afternoon'; return ( <main> <h1>{greetingMessage}, {userName}!</h1> <p>Current hour: {hour}</p> </main> ); } export default Greeting;
Important Notes
Only JavaScript expressions (not statements) can go inside curly braces.
Expressions inside JSX are automatically converted to strings or elements.
Use parentheses to wrap complex expressions for better readability.
Summary
Use curly braces {} to embed JavaScript expressions inside JSX.
Expressions can be variables, calculations, or conditional logic.
This makes your React UI dynamic and responsive to data changes.
Practice
1. In React JSX, how do you embed a JavaScript variable
name inside a <p> tag to display it?easy
Solution
Step 1: Understand JSX embedding syntax
In JSX, JavaScript expressions must be wrapped in curly braces{}to be evaluated and rendered.Step 2: Apply embedding to variable
To display the variablename, write it inside curly braces within the JSX tag:{name}.Final Answer:
<p>{name}</p> -> Option BQuick Check:
Use curly braces {} to embed variables [OK]
Hint: Use curly braces {} to embed variables in JSX [OK]
Common Mistakes:
- Using quotes instead of curly braces
- Trying to use template literals inside JSX directly
- Escaping curly braces incorrectly
2. Which of the following is the correct way to embed a JavaScript expression that adds two numbers inside JSX?
easy
Solution
Step 1: Recognize expression embedding
JSX requires expressions inside curly braces to evaluate them, so{2 + 3}will calculate the sum.Step 2: Identify correct syntax
{2 + 3} correctly uses curly braces with the expression2 + 3. Other options treat it as plain text or incorrect syntax.Final Answer:
<div>{2 + 3}</div> -> Option AQuick Check:
Curly braces evaluate expressions in JSX [OK]
Hint: Wrap expressions in {} to evaluate inside JSX [OK]
Common Mistakes:
- Putting expressions as plain text without braces
- Using template literals without braces
- Wrapping expressions in quotes
3. What will be rendered by this React component?
function Greeting() {
const hour = 10;
return <h1>Good {hour < 12 ? 'Morning' : 'Evening'}!</h1>;
}medium
Solution
Step 1: Evaluate the conditional expression
The variablehouris 10, which is less than 12, so the conditionhour < 12is true.Step 2: Determine the rendered string
Because the condition is true, the expression returns 'Morning', so the rendered output is<h1>Good Morning!</h1>.Final Answer:
<h1>Good Morning!</h1> -> Option DQuick Check:
Conditional expression inside {} renders correct text [OK]
Hint: Evaluate expressions inside {} before rendering [OK]
Common Mistakes:
- Thinking JSX shows raw expressions
- Confusing true/false in ternary
- Missing curly braces for expressions
4. Identify the error in this JSX snippet:
const count = 5;
return <div>You have {count} new messages</div>;medium
Solution
Step 1: Check expression embedding
The variablecountis correctly embedded inside curly braces{count}within JSX.Step 2: Verify JSX syntax
The JSX syntax is valid; parentheses around return are optional for single-line JSX.Final Answer:
No error, code is correct -> Option AQuick Check:
Curly braces correctly embed variables in JSX [OK]
Hint: Curly braces embed variables; parentheses optional for single JSX line [OK]
Common Mistakes:
- Thinking parentheses are mandatory for return
- Trying to escape curly braces unnecessarily
- Omitting curly braces around variables
5. Given this React component, what will be the output?
function ListItems() {
const items = ['Apple', 'Banana', 'Cherry'];
return (
<ul>
{items.map(item => <li key={item}>{item.length > 5 ? item : 'Short'}</li>)}
</ul>
);
}hard
Solution
Step 1: Evaluate each item length
Items are 'Apple'(5), 'Banana'(6), 'Cherry'(6). The conditionitem.length > 5is true for 'Banana' and 'Cherry', false for 'Apple'.Step 2: Apply conditional rendering
For 'Apple', length is 5 (not > 5), so it renders 'Short'. For 'Banana' and 'Cherry', it renders the item name.Step 3: Check keys and JSX structure
Each<li>has a unique key from the item string, so no error occurs.Final Answer:
<ul><li>Short</li><li>Banana</li><li>Cherry</li></ul> -> Option CQuick Check:
Conditional expression inside map renders correct list [OK]
Hint: Check condition inside map for each item before rendering [OK]
Common Mistakes:
- Assuming all items render their names
- Forgetting to add keys in list items
- Confusing length comparison operator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
