Course contentsShow
← React
Lesson 84 of 2,3963. JSX FundamentalsFree lesson

Interpolating Variables and Constants

Displaying variables and constants inside JSX by wrapping them in curly braces.

Interpolating Variables and Constants

What you'll learn: How to display dynamic JavaScript values inside your JSX by wrapping them in curly braces.

The Core Idea

Remember curly braces {} from the previous lesson? They're your escape hatch from JSX back into JavaScript. Now you'll use them for one of their most common purposes: displaying variables and constants.

When you write JSX, most of it looks like HTML. But what if you want to show a user's name, a calculated total, or any other value stored in JavaScript? You can't just type the variable name like it's text—JSX will treat it as literal characters. Instead, wrap the variable in curly braces to tell React: "Evaluate this as JavaScript and insert the result here."

How It Works

const userName = "Sarah";
const age = 28;

const element = <h1>Hello, {userName}! You are {age} years old.</h1>;

The values of userName and age get interpolated (inserted) into the JSX. When this compiles to React.createElement calls, those variables become arguments that React uses to build the actual DOM content.

Think of It Like Mad Libs

Remember fill-in-the-blank stories? JSX is similar: the HTML-like structure is the template, and curly braces are the blanks where you drop in JavaScript values. React fills them in when it renders.

const score = 42;
<p>Your score: {score}</p>  // Renders: Your score: 42

Key Takeaway: To display variables or constants in JSX, wrap them in curly braces {variableName}. This interpolates (inserts) their values into the rendered output.