Template literals

Text templates in backticks: insert values with ${} and span multiple lines.

Returns
A new string with the values of the expressions inserted in place of ${...}. A tagged template returns whatever the tag function returns.
On this page

Example

#
JavaScript
const name = 'Mia'
const points = 120
const goal = 200

console.log(`Hi, ${name}! You have ${points} pts.`)
console.log(`${goal - points} pts left to the goal.`)
Console
Hi, Mia! You have 120 pts.
80 pts left to the goal.

Definition and usage

#

Template literals are written in backticks `. Inside, you can insert any JavaScript expression in ${...}: a variable, a calculation, a function call, or a conditional operator. The result gets converted to text and inserted right there.

A template can span multiple lines. Newlines and indentation from the code end up in the string exactly as written, so you don’t need \n or string concatenation with +. That’s convenient for building messages and HTML fragments.

A function name written right before a template, e.g. highlight`You have ${points} pts`, creates a tagged template. The function receives the constant text pieces and the inserted values separately, so it can process them however it likes. The built-in String.raw tag returns the text without interpreting escape sequences like \n.

Syntax

#
Syntax
`text`
`text ${expression} text`
`line 1
line 2`
tag`text ${expression}`

More examples

#
Multi-line text
JavaScript
const course = 'JavaScript'
const lessons = 12

const card = `Course: ${course}
Lessons: ${lessons}
Status: ${lessons > 10 ? 'long' : 'short'}`

console.log(card)
Console
Course: JavaScript
Lessons: 12
Status: long
Building HTML from an array
JavaScript
const tasks = ['Variables', 'Loops']

const list = `<ul>${tasks.map((task) => `<li>${task}</li>`).join('')}</ul>`
console.log(list)
Console
<ul><li>Variables</li><li>Loops</li></ul>
A tagged template and String.raw
JavaScript
function highlight(strings, ...values) {
  return strings.reduce((result, text, i) => {
    const value = i < values.length ? `[${values[i]}]` : ''
    return result + text + value
  }, '')
}

const player = 'Jack'
const exp = 45

console.log(highlight`Player ${player} earned ${exp} XP`)
console.log(String.raw`C:\new\folder`)
Console
Player [Jack] earned [45] XP
C:\new\folder

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Don’t insert user data into HTML without sanitizing it. A template doesn’t escape < and >, so code like that opens the door to XSS attacks. Insert text through textContent instead.
  • In a multi-line template, the code’s indentation becomes part of the string. If that gets in the way, start further lines at the left edge, or join an array of lines with join("\n").
  • An object inserted with ${} turns into [object Object]. Insert a specific field instead, or use JSON.stringify().

Related entries

#

Spotted a mistake or missing an example? Write to us.