📖 Scenario: You are building a simple React component that shows a greeting message with a user's name and the current year.
🎯 Goal: Create a React functional component that uses JSX expressions to embed variables and JavaScript expressions inside the rendered output.
📋 What You'll Learn
Create a variable with the user's name
Create a variable with the current year
Use JSX expressions to embed these variables inside the returned JSX
Render a greeting message that includes the user's name and the current year
💡 Why This Matters
🌍 Real World
Embedding expressions in JSX is how React components show dynamic data, like user names, dates, or calculated values, making web pages interactive and personalized.
💼 Career
React developers frequently embed JavaScript expressions in JSX to build user interfaces that update automatically based on data or user input.
Progress0 / 4 steps
1
Create user name variable
Create a variable called userName and set it to the string "Alice" inside the Greeting component.
React
Hint
Use const userName = "Alice"; inside the component function.
2
Create current year variable
Add a variable called currentYear and set it to the number 2024 inside the Greeting component, below userName.
React
Hint
Use const currentYear = 2024; below userName.
3
Embed expressions in JSX
Inside the return statement, replace the comment with a <p> element that uses JSX expressions to embed userName and currentYear in this exact text: Hello, Alice! Welcome to the year 2024.
React
Hint
Use curly braces {} inside JSX to embed variables like {userName} and {currentYear}.
4
Complete component export
Ensure the component is exported as the default export with the exact name Greeting.
React
Hint
Make sure the component is exported with export default function Greeting().
Practice
(1/5)
1. In React JSX, how do you embed a JavaScript variable name inside a <p> tag to display it?
easy
A.
name
B.
{name}
C.
${name}
D.
\{name\}
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 variable name, write it inside curly braces within the JSX tag: {name}.
Final Answer:
<p>{name}</p> -> Option B
Quick 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
A.
{2 + 3}
B.
2 + 3
C.
${2 + 3}
D.
{'2 + 3'}
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 expression 2 + 3. Other options treat it as plain text or incorrect syntax.
Final Answer:
<div>{2 + 3}</div> -> Option A
Quick Check:
Curly braces evaluate expressions in JSX [OK]
Hint: Wrap expressions in {} to evaluate inside JSX [OK]