Template Literals
Backtick strings with variable interpolation, multiline text, and tagged templates.
What you'll learn
- Creating template literals with backticks
- Interpolating expressions with ${ ... }
- Writing multiline strings without escape sequences
- Nesting template literals for complex formatting
- Tagged templates for custom string processing
Concept
Backtick Strings
A template literal is a string delimited by backticks instead of quotes. Template literals solve two pain points of traditional strings: they allow multiline text without \n escapes, and they allow expression interpolation with the ${ ... } syntax.
Interpolation
Place any JavaScript expression inside ${ } and it is evaluated and converted to a string. This replaces the old concatenation style ("Hello " + name + "!") with a far more readable form: `Hello ${name}!`. You can interpolate variables, arithmetic, function calls, and even nested template literals.
Multiline Strings
With template literals, line breaks in the source are preserved in the output. This is ideal for HTML templates, SQL queries, and any text that spans multiple lines. To suppress leading whitespace, use a tag function or the common .trim() trick on the whole literal.
Tagged Templates
A tagged template calls a function with the string parts and interpolated values as separate arguments. This lets you preprocess the string — escape HTML, localize text, or build DSLs. Libraries like styled-components use tagged templates heavily. The tag function receives an array of string fragments and the interpolated values as individual arguments.
When to Use Them
Template literals are the modern default for any string that combines text with variables or spans multiple lines. Reserve single and double quotes for short, static strings where interpolation is not needed. Remember that backticks inside a template literal must be escaped if they appear in the content.
Example
const product = "Laptop";
const price = 999;
const qty = 2;
// Interpolation replaces concatenation
const receipt = `You bought ${qty} × ${product} for $${(price * qty).toFixed(2)}`;
console.log(receipt);
// Multiline HTML template
const card = `
<div class="card">
<h2>${product}</h2>
<p>Price: $${price}</p>
</div>
`.trim();
console.log(card);
// Nested template literals
const items = ["Apple", "Banana", "Cherry"];
const list = `Fruits:
${items.map((i) => ` - ${i}`).join("\n")}`;
console.log(list);
// Tagged template: escape HTML to prevent XSS
function escapeHtml(strings, ...values) {
const escape = (s) =>
String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
return strings.reduce((acc, str, i) => acc + str + escape(values[i] ?? ""), "");
}
const userInput = "<script>alert(1)</script>";
console.log(escapeHtml`Comment: ${userInput}`);
Shows interpolation with toFixed, a multiline HTML template trimmed, nested template literals inside map, and a tagged template that escapes HTML to prevent XSS.
Try it
- HTML ↔ Markdown
Template literals often build HTML — convert between HTML and Markdown here.
- Case Converter
Transform interpolated text into different naming conventions.
Common mistakes
The mistake
Forgetting to escape backticks inside a template literal
The fix
If your string content contains a backtick, escape it with a backslash. Alternatively, use String.raw or a different quoting strategy for the content.
The mistake
Interpolating user input into HTML without escaping
The fix
Never inject untrusted data into HTML via template literals directly. Use a tagged template that escapes HTML, or rely on a framework like React that escapes by default.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Template literal syntax and tagged template examples.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.