Expressions in JSX
You want a paragraph to greet someone by name. If you type the name straight into the markup, every visitor sees the same word. JSX needs a way to drop a changing value into the middle of your output.
JSX is not a template language — it is JavaScript. Anywhere you want a value, you open a pair of curly braces and write an expression:
const name = 'Ada';
const total = 2 + 2;
return (
<p>
Hello, {name}! Two plus two is {total}.
</p>
);
Everything inside {} is evaluated as JavaScript and its result is rendered.
You can reference variables, do arithmetic, call functions, or use a ternary —
anything that produces a value.
Curly braces also work in attributes, where they let you pass a value instead of a string:
<img src={avatarUrl} alt={name} />
A common mistake is to write the expression as text. <p>2 + 2</p> renders the
literal characters 2 + 2; <p>{2 + 2}</p> renders 4. The braces are what
tell React to evaluate rather than print.
Your job is to render the result of an expression, not the expression itself.
Challenge
A paragraph is meant to show a sum, but it prints the characters 2 + 2 instead
of the answer. Export an App component from App.jsx that renders a
paragraph showing the result of 2 + 2 — the text on screen should read 4.
The Test renders your component and reads the paragraph's text. The starter
prints the literal characters 2 + 2, so the Test fails — wrap the expression in
curly braces so React evaluates it.