Programming Search⌘K

Featureintro

Template literals

Also called template strings, backtick strings, string interpolation in JavaScript, multi-line strings in JavaScript, placeholders, template substitution.

A template literal is a string in backticks. Each ${expression} placeholder is evaluated, converted to a string and joined with the text around it, and the text may span several lines.

Language
JavaScript

Synopsis

const item = "tea";
const price = 3.5;
console.log(`One ${item} costs $${price.toFixed(2)}.`);
console.log(`2 + 3 = ${2 + 3}, upper: ${item.toUpperCase()}`);
const lines = `first
second`;
console.log(lines.split("\n"), lines.length);
console.log(`a backtick: \`, a placeholder: \${x}`);
Output
One tea costs $3.50.
2 + 3 = 5, upper: TEA
[ 'first', 'second' ] 12
a backtick: `, a placeholder: ${x}
Part Example Notes
text `plain` the same escapes as in quoted strings
placeholder `${a + b}` any expression; the value is converted to a string
line break a new line in the source is part of the string
escaped backtick `\ `` a literal backtick
escaped dollar `\${x}` the text ${x}, no placeholder

Semantics

r1. Template literals are delimited with backtick characters, "allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates."1 A template literal can "contain other parts called placeholders, which are embedded expressions delimited by a dollar sign and curly braces: ${expression} ."1 Example: basics.

r2. Without a tag, "The default function (when you don't supply your own) just performs string interpolation to do substitution of the placeholders and then concatenate the parts into a single string."1 The specification evaluates each placeholder and takes ToString of its value: "Let middle be ? ToString(sub)."2 With a function name before the backtick, "the result is called a tagged template".1 See Tagged templates.

r3. "Template literals coerce their expressions directly to strings, while addition coerces its operands to primitives first."1 The specification says the same: "The string conversion semantics applied to the Expression value are like String.prototype.concat rather than the + operator."2 So an object with both valueOf and toString can give different text in a template and with +. Example: coerce.

r4. "Any newline characters inserted in the source are part of the template literal."1 Line endings are made the same: in the value of a template, a carriage return and line feed pair, or a carriage return alone, becomes a line feed, and an escape such as \r is needed to keep a carriage return.3 Example: indent.

r5. "To escape a backtick in a template literal, put a backslash (\ ) before the backtick." "Dollar signs can be escaped as well to prevent interpolation."1 An untagged template does not allow a bad escape: "invalid escape sequences will cause a syntax error, unless a tag function is used".1 See String escape sequences.

r6. Templates nest: "Within a backtick-delimited template, it is simple to allow inner backticks by using them inside an ${expression} placeholder within the template."1 Example: nested.

Examples

A template inside a placeholder (rule r6).

const items = ["pen", "ink"];
const list = `<ul>${items.map((i) => `<li>${i}</li>`).join("")}</ul>`;
console.log(list);
const files = (n) => `${n} file${n === 1 ? "" : "s"}`;
console.log(files(1), files(2));
Output
<ul><li>pen</li><li>ink</li></ul>
1 file 2 files

A template and + convert an object in different ways (rule r3).

const money = { valueOf: () => 42, toString: () => "$42" };
console.log(`${money}`, "" + money, String(money));
console.log(`${[1, 2]}`, `${null}`, `${undefined}`, `${{}}`);
Output
$42 42 $42
1,2 null undefined [object Object]

Pitfalls

Indentation inside a multi-line template

The spaces at the start of each line are part of the string (rule r4).

This code runs but gives a wrong answer on purpose.

function usage() {
  return `Usage:
    run <file>`;
}
console.log(JSON.stringify(usage()));

const fixed = ["Usage:", "  run <file>"].join("\n");
console.log(JSON.stringify(fixed));
Output
"Usage:\n    run <file>"
"Usage:\n  run <file>"

A placeholder in quotes

${...} is only a placeholder between backticks. In single or double quotes it is plain text.

This code runs but gives a wrong answer on purpose.

const name = "Ada";
console.log('Hello, ${name}');
console.log(`Hello, ${name}`);
Output
Hello, ${name}
Hello, Ada

Comparison

Python's f-strings are the closest match: "A formatted string literal or f-string is a string literal that is prefixed with" f or F, and "They may contain replacement fields delimited by curly braces {} . Replacement fields contain expressions which are evaluated at run time."4 In JavaScript the backticks mark the template, with no prefix letter (rule r1). C# uses a $ before the string: "An interpolated string is a string literal that might contain interpolation expressions."5 See String interpolation, Interpolate values into a string and Formatted String Literals.

References

  1. MDN Web Docs, JavaScript Reference, Template literals, the introduction; Description; Multi-line strings; String interpolation; Nesting templates
  2. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 13, 13.2.8.6 Runtime Semantics, Evaluation
  3. ECMAScript 2026 Language Specification (ECMA-262, 17th edition), clause 12, 12.9.6 Template Literal Lexical Components
  4. The Python Language Reference, Python 3.14, Lexical analysis, 2.5.7 f-strings
  5. C# language reference, String interpolation using $, the introduction

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source