01 / 20
` $ `

Template
Literals

Master the modern way to build dynamic strings in JavaScript using backticks, expressions, and multi-line text.

JavaScript

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.

Backtick Syntax
Wrapped in `backticks` instead of single or double quotes
Embedded Expressions
Insert variables and code using ${...} placeholders
Multi-line
Span across multiple lines without escape characters

The Old Way vs. The New Way

Old way: string concatenation
const name = "Alice";
const greeting = "Hello, " + name + "!";
New way: template literal
const name = "Alice";
const greeting = `Hello, ${name}!`;
Hello, Alice!

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.

1
Readability: You can see the final string shape at a glance, without + signs cluttering the code.
2
Fewer bugs: No more forgetting to add spaces or accidentally joining values without separators.
3
Multi-line support: Create HTML templates and formatted text without awkward \n characters.

Anatomy of a Template Literal

` Score: ${points} out of ${total} ` Backtick Static text Expression ${...} Backtick "Score: 85 out of 100"

The Core Rule

// Variables
`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.

const html = `
  <div class="card">
    <h2>${title}</h2>
    <p>${description}</p>
  </div>
`
;

How to Apply Template Literals

1
Open with a backtick ` (found below the Escape key on most keyboards).
2
Type your static text exactly as you want it to appear in the final string.
3
Insert dynamic values using ${expression} wherever you need a computed value.
4
Close with a backtick ` to complete the template literal.
5
Test your output with console.log() to verify the result looks right.

Example 1: Simple Interpolation

const item = "coffee";
const price = 4.5;

const receipt = `Your ${item} costs $${price.toFixed(2)}.`;

console.log(receipt);
Your coffee costs $4.50.

Example 2: Expressions & Logic

const score = 87;
const total = 100;
const passMark = 60;

const result = `You scored ${score}/${total}. ${score >= passMark ? "You passed!" : "Try again."}`;

console.log(result);
You scored 87/100. You passed!

Example 3: Building HTML Dynamically

const users = ["Alice", "Bob", "Carol"];

const listHTML = `
  <ul>
    ${users.map(u => `<li>${u}</li>`).join("")}
  </ul>
`
;
<ul><li>Alice</li><li>Bob</li><li>Carol</li></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.

function highlight(strings, ...values) {
  return strings.reduce((acc, str, i) =>
    acc + str + (values[i] ? `<mark>${values[i]}</mark>` : '')
  , "");
}

highlight`Hello ${name}, welcome!`;

Common Mistakes to Avoid

✕ Using Regular Quotes
Writing "Hello ${name}" with double quotes outputs the literal text ${name} instead of the variable's value. You must use backticks.
✕ Forgetting the Dollar Sign
Writing `Hello {name}` without the $ treats {name} as plain text, not an expression.
✕ Unescaped Backticks
If your string contains a literal backtick character, you must escape it with a backslash: \` inside the template literal.

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?

const x = 5;
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?