Template
Literals
Master the modern way to build dynamic strings in JavaScript using backticks, expressions, and multi-line text.
What Are Template Literals?
Template literals are a modern string syntax introduced in ES6 (2015). Instead of regular quotes, they use backticks ` and unlock powerful features that plain strings cannot do.
The Old Way vs. The New Way
const greeting = "Hello, " + name + "!";
const greeting = `Hello, ${name}!`;
Why Do They Matter?
String building is one of the most common tasks in programming. Template literals make it cleaner, safer, and easier to read compared to manual concatenation.
Anatomy of a Template Literal
The Core Rule
`Name: ${firstName}`
// Arithmetic
`Total: ${price * quantity}`
// Function calls
`Upper: ${name.toUpperCase()}`
// Ternary
`Status: ${age >= 18 ? "adult" : "minor"}`
Quotes vs. Backticks
| Regular Quotes | Backticks | |
|---|---|---|
| Delimiters | ' ' or " " | ` ` |
| Interpolation | ✕ Not supported | ✓ ${expr} |
| Multi-line | ✕ Needs \n | ✓ Natural line breaks |
| Concatenation | Manual with + | Built-in |
| Readability | Decreases with complexity | Stays clear |
Multi-Line Strings
With regular quotes, you need \n or concatenation. With backticks, just press Enter and the line break is preserved.
<div class="card">
<h2>${title}</h2>
<p>${description}</p>
</div>
`;
How to Apply Template Literals
Example 1: Simple Interpolation
const price = 4.5;
const receipt = `Your ${item} costs $${price.toFixed(2)}.`;
console.log(receipt);
Example 2: Expressions & Logic
const total = 100;
const passMark = 60;
const result = `You scored ${score}/${total}. ${score >= passMark ? "You passed!" : "Try again."}`;
console.log(result);
Example 3: Building HTML Dynamically
const listHTML = `
<ul>
${users.map(u => `<li>${u}</li>`).join("")}
</ul>
`;
Tagged Template Literals
A tagged template lets you run a function on the template's parts before the final string is assembled. The function receives the static strings and the evaluated expressions as separate arguments.
return strings.reduce((acc, str, i) =>
acc + str + (values[i] ? `<mark>${values[i]}</mark>` : '')
, "");
}
highlight`Hello ${name}, welcome!`;
Common Mistakes to Avoid
Summary Comparison
| Feature | Concatenation | Template Literal |
|---|---|---|
| Syntax | "..." + var | `...${var}` |
| Multi-line | Requires \n | Natural line breaks |
| Expressions | Separate statements | Inline ${...} |
| HTML Building | Messy and error-prone | Clean and readable |
| Tagged Templates | ✕ Not possible | ✓ Supported |
| ES Version | ES1 (1997) | ES6 (2015) |
Question 1 of 5
Which character is used to delimit a template literal in JavaScript?
Question 2 of 5
What does the following code output?
console.log(`Result: ${x * 3}`);
Question 3 of 5
Why does "Hello ${name}" (with double quotes) not interpolate the variable?
Question 4 of 5
Which is not a valid expression inside ${...}?
Question 5 of 5
What advantage do multi-line template literals have over concatenation with \n?