Template Literals
for Formatting
Master the modern JavaScript syntax for building dynamic, readable strings using backticks and ${...} expressions.
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.
// Regular string const old = 'Hello, world!'; // Template literal const modern = `Hello, world!`;
Why Do They Matter?
Anatomy of a Template Literal
The Core Rule
`text ${expression} more text`
Concatenation vs Template Literals
const name = 'Alice'; const age = 17; const msg = 'Hi, I am ' + name + ' and I am ' + age + ' years old.';
const msg = `Hi, I am ${name} and I am ${age} years old.`;
What Goes Inside ${...}?
${username}${price * qty}${name.toUpperCase()}${age >= 18 ? 'Yes' : 'No'}Multi-line Strings
Template literals preserve line breaks naturally. No need for \n or concatenation across lines.
const poem = `Roses are red, Violets are blue, JavaScript is great, And so are you.`;
Roses are red, Violets are blue, JavaScript is great, And so are you.
Step-by-Step Approach
Example 1: Simple Greeting
const firstName = 'Maya'; const course = 'Computer Science'; const greeting = `Welcome, ${firstName}! You are enrolled in ${course}.`; console.log(greeting);
Welcome, Maya! You are enrolled in Computer Science.
Example 2: Inline Math
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);
3x Notebook @ $4.99 each Total: $14.97
Example 3: Conditional Logic
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);
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.
const isVIP = true; const user = 'Sam'; const badge = `${user} ${isVIP ? `${'★'.repeat(3)}` : ''}`; // Result: "Sam ★★★"
Common Mistakes
'Hello ${name}' with single quotes does not interpolate: it prints the literal text "${name}".`Hello {name}` without the dollar sign just prints "{name}" as plain text.${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}`);