Docs
13.2-Template-Literals
17.2 Template Literals
Overview
Template literals (template strings) are string literals that allow embedded expressions, multi-line strings, and string interpolation. They use backticks (`) instead of quotes.
Basic Syntax
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Template Literals ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā // Traditional strings ā
ā const str1 = 'Hello, ' + name + '!'; ā
ā const str2 = "Hello, " + name + "!"; ā
ā ā
ā // Template literal ā
ā const str3 = `Hello, ${name}!`; ā
ā ā
ā // Multi-line (traditional - escaped) ā
ā const multi1 = 'Line 1\n' + ā
ā 'Line 2\n' + ā
ā 'Line 3'; ā
ā ā
ā // Multi-line (template literal) ā
ā const multi2 = `Line 1 ā
ā Line 2 ā
ā Line 3`; ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
String Interpolation
const name = 'Alice';
const age = 30;
// Expression interpolation
console.log(`Name: ${name}, Age: ${age}`);
// Any expression works
console.log(`Next year: ${age + 1}`);
console.log(`Uppercase: ${name.toUpperCase()}`);
console.log(`Even? ${age % 2 === 0 ? 'Yes' : 'No'}`);
// Function calls
console.log(`Random: ${Math.random().toFixed(2)}`);
Multi-line Strings
// HTML templates
const html = `
<div class="card">
<h2>${title}</h2>
<p>${content}</p>
</div>
`;
// Preserves whitespace and newlines
const poem = `
Roses are red,
Violets are blue,
Template literals,
Are awesome too!
`;
Nesting Template Literals
const items = ['apple', 'banana', 'cherry'];
const list = `
<ul>
${items.map((item) => `<li>${item}</li>`).join('\n ')}
</ul>
`;
Tagged Templates
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Tagged Templates ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā function tag(strings, ...values) { ā
ā // strings: array of string literals ā
ā // values: array of interpolated values ā
ā console.log(strings); // ['Hello, ', '! You are ', ' years old'] ā
ā console.log(values); // ['Alice', 30] ā
ā return strings[0] + values[0] + strings[1] + values[1]; ā
ā } ā
ā ā
ā const result = tag`Hello, ${name}! You are ${age} years old`; ā
ā ā
ā // Note: No parentheses needed after function name ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Common Tagged Template Uses
| Use Case | Description |
|---|---|
| HTML escaping | Prevent XSS attacks |
| Internationalization | i18n string translation |
| CSS-in-JS | styled-components, emotion |
| SQL queries | Parameterized queries |
| Syntax highlighting | Code formatting |
Raw Strings
// String.raw - ignores escape sequences
console.log(String.raw`Line1\nLine2`);
// Output: 'Line1\nLine2' (literal backslash-n)
// Useful for regex, file paths
const regex = String.raw`\d+\.\d+`;
const path = String.raw`C:\Users\name\Documents`;
Comparison with Traditional Strings
| Feature | Traditional | Template Literal |
|---|---|---|
| Delimiter | ' or " | ` |
| Interpolation | + var + | ${var} |
| Multi-line | \n or concat | Natural newlines |
| Raw access | N/A | String.raw |
| Tagged | N/A | tag\...`` |
Summary
- ā¢Use backticks for template literals
- ā¢
${expression}for interpolation - ā¢Natural multi-line support
- ā¢Tagged templates for custom processing
- ā¢
String.rawfor raw strings