CKTIS / JavaScript
Template literals in JavaScript
Build multiline strings and insert expression results. Learn when JavaScript produces a string and when it passes text segments and values to a tag function.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
`text`
`text ${expression} more text`
tag`text ${expression} more text`
String.raw`text ${expression} more text`Template literals use backticks instead of quotes. A placeholder ${expression} contains an expression. In an untagged literal, JavaScript evaluates the expressions from left to right, converts each result to a string, and joins it with the text. Directly interpolating a Symbol value throws TypeError. Line breaks and indentation in the literal text become part of the string. Use \` to include a backtick and \${ to include ${ without starting a placeholder.
Putting a function directly before the opening backtick calls it as a tag. Its first argument is a frozen array of processed (cooked) text segments, including empty ones. The remaining arguments are the expression values, without automatic string conversion. The tag's return value is the result of the whole expression and need not be a string. Repeated evaluations of the same literal site in the same realm reuse the same array rather than making a copy.
In a cooked segment, \n represents a newline. The frozen parts.raw array preserves the backslash and the letter n there. An invalid escape such as \xZ causes a SyntaxError in an untagged literal; with a tag, the affected cooked segment is undefined and the raw spelling is still available. The built-in tag String.raw joins raw segments with substitution values converted to strings.
Interpolation does not escape data for HTML, and adding a tag does not by itself make the result safe. To display untrusted data as plain text in an ordinary text-display element such as <p> or <span>, use textContent, not innerHTML. On a <script> element, setting textContent sets the script source rather than displaying text. Template literals are ECMAScript syntax and do not require the DOM; check support in your target runtime.
Examples
{
const count = 5;
const report = `Items: ${count}
Status: ready`;
function inspect(parts, value) {
return [parts[0], parts.raw[0], value];
}
console.log(JSON.stringify(report));
console.log(JSON.stringify(inspect`Items:\n${count}`));
}Result
"Items: 5\nStatus: ready" ["Items:\n","Items:\\n",5]
The first line shows a multiline string. The second compares the cooked and raw segments. `JSON.stringify` makes the difference between a newline and a literal `\n` visible; the number `5` remains unquoted.
Related lessons
- JavaScript · Lesson 1 · Advanced · Version: 4
- JavaScript · Lesson 4 · Advanced · Version: 4
- JavaScript · Lesson 5 · Advanced · Version: 4
- JavaScript · Lesson 6 · Advanced · Version: 4
- JavaScript · Lesson 7 · Advanced · Version: 4
- JavaScript · Lesson 8 · Advanced · Version: 4
- JavaScript · Lesson 10 · Advanced · Version: 4
- JavaScript · Lesson 11 · Advanced · Version: 4
- JavaScript · Lesson 14 · Advanced · Version: 4
- JavaScript · Lesson 16 · Advanced · Version: 4
- JavaScript · Lesson 17 · Advanced · Version: 4
- JavaScript · Lesson 18 · Advanced · Version: 4
- JavaScript · Lesson 17 · Intermediate · Version: 3
- JavaScript · Lesson 19 · Intermediate · Version: 3
- JavaScript · Lesson 20 · Intermediate · Version: 3
- JavaScript · Lesson 21 · Intermediate · Version: 3
- JavaScript · Lesson 22 · Intermediate · Version: 3
- JavaScript · Lesson 23 · Intermediate · Version: 3
- JavaScript · Lesson 25 · Intermediate · Version: 3
- JavaScript · Lesson 28 · Intermediate · Version: 3
- JavaScript · Lesson 30 · Intermediate · Version: 3
- JavaScript · Lesson 31 · Intermediate · Version: 3
- JavaScript · Lesson 33 · Intermediate · Version: 3
- JavaScript · Lesson 35 · Intermediate · Version: 3
- JavaScript · Lesson 38 · Intermediate · Version: 3
- JavaScript · Lesson 42 · Intermediate · Version: 3