Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

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 stock

Multi-line text:

const message = `Line one
Line two`;
 
console.log(message);

Output:

Line one
Line two

Notes

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

In the tutorial

  • Template literals — Template literals in JavaScript: inserting values and expressions, multi-line text, conditions inside a template and tagged templates.

Thanks for reading!

Donate to the site — we work hard for you.