Template literals
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates.
Template literals are sometimes informally called template strings, because they are used most commonly for string interpolation (to create strings by doing substitution of placeholders). However, a tagged template literal may not result in a string; it can be used with a custom tag function to perform whatever operations you want on the different parts of the template literal.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 41 | 12 | 34 | 9 | 41 | 9 | |
template literal revision Escape sequences allowed in tagged template literals | 62 | 79 | 53 | 11 | 62 | 11 |
Syntax
const name = 'Taro';
const age = 25;
// Embedding expressions
console.log(`Hello, ${name}!`);
console.log(`Age: ${age}, Next year: ${age + 1}`);
// Multiple lines
const html = `
<div>
<h1>${name}</h1>
<p>Age: ${age}</p>
</div>
`; Live demo
Interpolation with expressions
Embed variables, calculations, and conditional text directly inside one readable string.
Multiline templates
Keep the structure of generated text close to the final output by using a multiline literal.
Use cases
-
Formatting messages
Build UI strings, logs, and summaries with embedded expressions that stay close to the sentence structure.
-
Generating structured text
Create HTML fragments, emails, or reports that benefit from multiline layout in source code.
Cautions
- Template literals can still produce unsafe HTML if you inject untrusted values into markup strings.
- Long nested expressions inside ${...} become hard to read; compute them first when needed.
Accessibility
- If template strings generate labels or announcements, keep the final text concise and predictable for assistive technologies.