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

Template Literals Inside JSX

When and how to use template literals within curly braces for complex string construction.

Template Literals Inside JSX

What you'll learn: How to use JavaScript's template literal syntax inside curly braces to build complex strings within your JSX.

Why Template Literals in JSX?

You already know how to interpolate variables with curly braces ({name}), and you can concatenate strings with the + operator ({firstName + " " + lastName}). But when you need to construct strings with multiple variables or complex formatting, template literals offer a cleaner, more readable solution.

The Syntax

Template literals use backticks (`) instead of quotes, and let you embed expressions directly using ${}:

const name = "Alice";
const score = 95;

// Without template literals (works, but clunky):
<p>{"Player: " + name + ", Score: " + score}</p>

// With template literals (cleaner):
<p>{`Player: ${name}, Score: ${score}`}</p>

When to Use Them

Template literals shine when you're:

  • Combining multiple variables: {Hello firstName{firstName} {lastName}!}
  • Including calculations: {Total: $${price * quantity}}
  • Building URLs or paths: {/users/${userId}/profile}
  • Multi-line strings: They preserve line breaks naturally

Inside the Curly Braces

Remember: the curly braces are your "JavaScript zone." Inside them, you write normal JavaScript. A template literal is just JavaScript, so it fits perfectly:

const user = "Sam";
const items = 3;

<h1>{`Welcome back, ${user}! You have ${items} items.`}</h1>

Think of template literals as a string formatter — like a form with blank fields where JavaScript expressions get inserted.

Key Takeaway: Template literals (backticks with ${}) inside curly braces make complex string construction in JSX readable and maintainable, especially when combining multiple variables or expressions.