CKTIS / JavaScript
Template literal syntax
Template literals let you write multiline text and interpolate expressions. A tagged template passes text segments and substitution values separately to a function.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
`text`
`first line
second line`
`text ${expression} more text`
tag`text ${expression} more text`Template literal syntax uses backticks instead of single or double quotes. Ordinary line breaks and indentation in the text become part of the result. To write a literal backtick, precede it with a backslash. Write \${ to include ${ without starting a substitution.
In an untagged literal, JavaScript evaluates the expressions in ${expression} from left to right, converting each value to a string before moving to the next expression. The result is a string. Interpolating a Symbol value directly throws TypeError; use String(value) explicitly if you want its text representation.
In a tagged template, tag`...` calls a function without parentheses. Its first argument, parts, is a frozen array of cooked text segments, including empty ones: n substitutions produce n + 1 segments. The remaining arguments are the substitution values, with no automatic string conversion. The expression takes whatever value the tag returns. Repeated evaluation at the same source site in the same JavaScript realm reuses the same array.
The frozen parts.raw array contains the segments without interpreting escape sequences. Physical CRLF and CR line endings in the source become LF in both cooked and raw segments. An invalid escape such as \xG is a SyntaxError without a tag; with a tag, the affected cooked segment is undefined and the raw segment retains the sequence.
This is JavaScript syntax, not a DOM API; check support in your target runtime. Interpolation does not escape values for HTML. To display untrusted text in an ordinary page element, use textContent, not innerHTML.
Examples
const count = 2;
const message = `Total: ${count + 1}
Ready`;
function inspect(parts, value) {
return [parts[1].startsWith("\n"), parts.raw[1].startsWith("\\n"), typeof value];
}
console.log(message);
console.log(JSON.stringify(inspect`Total: ${count + 1}\nReady`));Result
Total: 3 Ready [true,true,"number"]
The untagged literal prints two lines. In the tag's result, the first `true` means the cooked segment starts with a newline; the second means the raw segment starts with two characters, a backslash and `n`. `"number"` confirms that the tag received a number.
Related lessons
- JavaScript · Lesson 23 · Advanced · Version: 4
- JavaScript · Lesson 24 · Advanced · Version: 4
- JavaScript · Lesson 25 · Advanced · Version: 4
- JavaScript · Lesson 31 · Advanced · Version: 4
- JavaScript · Lesson 32 · Advanced · Version: 4
- JavaScript · Lesson 35 · Advanced · Version: 4
- JavaScript · Lesson 40 · Advanced · Version: 4
- JavaScript · Lesson 41 · Advanced · Version: 4
- JavaScript · Lesson 42 · Advanced · Version: 4
- JavaScript · Lesson 43 · Advanced · Version: 4