Template literal
A string in backticks with substitutions and line breaks.
Available since: ES2015
Syntax
`text ${expression} text`Examples
Inserting a variable:
const name = 'Anna';
console.log(`Hello, ${name}!`);Output:
Hello, Anna!Any expression goes inside the curly braces:
const price = 4;
const count = 3;
console.log(`Total: $${price * count}`);
console.log(`Status: ${count > 0 ? 'in stock' : 'sold out'}`);Output:
Total: $12
Status: in stockMulti-line text:
const message = `Line one
Line two`;
console.log(message);Output:
Line one
Line twoNotes
The backtick key is usually left of the 1 key. A ${...} substitution works only inside backticks — in single and double quotes it is plain text.
See also
- String.raw — A template literal without escape-sequence processing.
- String.prototype.concat — Joins strings together.
- String.prototype.repeat — Repeats a string a given number of times.
In the tutorial
- Template literals — Template literals in JavaScript: inserting values and expressions, multi-line text, conditions inside a template and tagged templates.