Skip to content

template literal

A template literal is a string literal written between backticks that can span several lines and insert the values of expressions through ${} placeholders.

When the literal runs, each placeholder’s expression is evaluated, converted to a string, and joined with the text around it, so the result is an ordinary string. Line breaks typed between the backticks become part of the value, so multi-line text needs no \n escapes. Template literals arrived in ES2015 and show up wherever code builds text from data, such as log messages, URLs, and HTML fragments.

Each placeholder value goes through string type coercion. Numbers and Booleans turn into their usual text, and null and undefined turn into the words "null" and "undefined". A plain object becomes "[object Object]", and a symbol throws a TypeError.

A function placed right before the opening backtick turns the literal into a tagged template. The function receives the text pieces and the placeholder values separately and decides what to return. The built-in String.raw tag, for example, keeps backslashes exactly as written.

Example

Say an online store builds an order confirmation for a customer:

Language: JavaScript
> const customer = "Ada";
undefined
> const items = 3;
undefined
> const total = 42.5;
undefined

> `Thanks, ${customer}! Your ${items} items cost $${total.toFixed(2)}.`
'Thanks, Ada! Your 3 items cost $42.50.'

> `Order for ${customer}
… Items: ${items}`
'Order for Ada\nItems: 3'

Each placeholder evaluates its expression, including the method call total.toFixed(2). The first dollar sign in $${ stays plain text because no brace follows it. The second literal spans two lines, and the REPL shows the line break as \n inside the resulting string.

Say a log line interpolates a whole user object instead of one of its properties:

Language: JavaScript
> const user = { name: "Ada", role: "admin" };
undefined

> `Signed in: ${user}`
'Signed in: [object Object]'
> `Signed in: ${user.name}`
'Signed in: Ada'

> `Roles: ${["admin", "editor"]}`
'Roles: admin,editor'

The plain object inherits its .toString() from Object.prototype through the prototype chain, and that method returns "[object Object]". An array converts by joining its elements with commas, so the roles appear without spaces.

A developer holds up a LOGIN tile beside a clay machine that splits FIX LOGIN BUG into words, slices and trims tiles, and joins them into FIX-LOGIN-BUG.

Course

JavaScript Strings for Pythonistas

Port a payments team's branch-name helper to JavaScript, and learn where Python's string habits, from slicing to split() and join(), run without an error and give the wrong text.

basics js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 18, 2026