01 / 20
${ }

Template Literals
for Formatting

Master the modern JavaScript syntax for building dynamic, readable strings using backticks and ${...} expressions.

JavaScript

What Are Template Literals?

Template literals are a special kind of string in JavaScript, introduced in ES6. Instead of using quotes (' or "), you wrap your text in backticks (`). This unlocks powerful formatting features that regular strings cannot do.

JS
// Regular string
const old = 'Hello, world!';

// Template literal
const modern = `Hello, world!`;

Why Do They Matter?

Embedded Expressions
Insert variables and computations directly inside strings with ${...}
Multi-line Strings
Write strings that span multiple lines without escape characters like \n
Readability
No more messy concatenation with + operators everywhere

Anatomy of a Template Literal

`Hello, ${name}!` Opening backtick Static text Expression placeholder Closing backtick

The Core Rule

Syntax
`text ${expression} more text`
1
Wrap with backticks instead of single or double quotes
2
Insert ${...} wherever you want a dynamic value
3
Any valid expression goes inside the braces: variables, math, function calls, ternaries

Concatenation vs Template Literals

Old Way
const name = 'Alice';
const age = 17;

const msg = 'Hi, I am ' + name
  + ' and I am ' + age
  + ' years old.';
New Way
const msg = `Hi, I am ${name} and I am ${age} years old.`;

What Goes Inside ${...}?

Variables
${username}
Arithmetic
${price * qty}
Function Calls
${name.toUpperCase()}
Ternaries
${age >= 18 ? 'Yes' : 'No'}

Multi-line Strings

Template literals preserve line breaks naturally. No need for \n or concatenation across lines.

JS
const poem = `Roses are red,
Violets are blue,
JavaScript is great,
And so are you.`;
Output
Roses are red,
Violets are blue,
JavaScript is great,
And so are you.

Step-by-Step Approach

1
Identify your data: What variables or values need to appear in the string?
2
Open with a backtick: Type the ` character to start your template
3
Write the static text: Type the fixed parts of your message
4
Insert ${} at each dynamic spot: Place variables or expressions inside the braces
5
Close with a backtick: Type the closing ` to finish the template

Example 1: Simple Greeting

JS
const firstName = 'Maya';
const course = 'Computer Science';

const greeting = `Welcome, ${firstName}!
You are enrolled in ${course}.`;

console.log(greeting);
Output
Welcome, Maya!
You are enrolled in Computer Science.

Example 2: Inline Math

JS
const item = 'Notebook';
const price = 4.99;
const qty = 3;

const receipt = `${qty}x ${item} @ \$${price} each
Total: \$${(price * qty).toFixed(2)}`;

console.log(receipt);
Output
3x Notebook @ $4.99 each
Total: $14.97

Example 3: Conditional Logic

JS
const score = 85;

const result = `You scored ${score}/100.
Grade: ${score >= 90 ? 'A' : score >= 80 ? 'B' : 'C'}
Status: ${score >= 60 ? 'PASS' : 'FAIL'}`;

console.log(result);
Output
You scored 85/100.
Grade: B
Status: PASS

Nesting Template Literals

You can even nest template literals inside other template literals. This is useful for conditional formatting.

JS
const isVIP = true;
const user = 'Sam';

const badge = `${user} ${isVIP ? `${'★'.repeat(3)}` : ''}`;
// Result: "Sam ★★★"

Common Mistakes

✕ Using Regular Quotes
Writing 'Hello ${name}' with single quotes does not interpolate: it prints the literal text "${name}".
✕ Forgetting the $
Writing `Hello {name}` without the dollar sign just prints "{name}" as plain text.
✕ Statements in ${}
Putting ${if (x) ...} inside a placeholder causes a syntax error. Use a ternary instead.

Comparison Summary

Concatenation (+) Template Literals
Delimiters ' ' or " " ` ` (backticks)
Insert Variables ' ... ' + var + ' ... ' `... ${var} ...`
Multi-line Requires \n Natural line breaks
Expressions Separate before concat Inline with ${...}
Readability Low High
Error-Prone Yes (missing quotes/spaces) No

Question 1 of 5

Which character is used to delimit (wrap) a template literal in JavaScript?

Question 2 of 5

Given const x = 5;, what does \(`Result: ${x * 2}`\) produce?

Question 3 of 5

What is wrong with this code?
'Hello, ${name}!'

Question 4 of 5

Which of the following can not be placed inside ${...} in a template literal?

Question 5 of 5

Given the code below, what is logged?

const a = 3;
const b = 7;
console.log(`Sum: ${a + b}, Product: ${a * b}`);