Open Playground
JS TutorialLesson #30

JavaScript String Templates

Beloved child has many names

Template Strings

Template Literals

Beloved child has many names

Back-Tics Syntax

Template Strings use back-ticks (``) rather than the quotes ("") to define a string:

Example

Example Code

javascript
Try it Yourself
let text = `Hello World!`;

Quotes Inside Strings

Template Strings allow both single and double quotes inside a string:

Example

Example Code

javascript
Try it Yourself
let text = `He's often called "Johnny"`;

Multiline Strings

Template Strings allow multiline strings:

Example

Example Code

javascript
Try it Yourself
let text =

`The quick

brown fox

jumps over

the lazy dog`;

Interpolation

Template Strings allow variables in strings.

Template strings provide an easy way to interpolate variables in strings.

Syntax

Example Code

javascript
Try it Yourself
${...}

Example

Example Code

javascript
Try it Yourself
let firstName = "John";

let lastName = "Doe";

let text = `Welcome ${firstName}, ${lastName}!`;

Expression Substitution

Template Strings allow interpolation of expressions in strings:

Example

Example Code

javascript
Try it Yourself
let price = 10;

let VAT = 0.25;

let total = `Total: ${(price * (1 + VAT)).toFixed(2)}`;

HTML Templates

Example

Example Code

javascript
Try it Yourself
let header = "Template Strings";

let tags = ["template strings", "javascript", "es6"];

let html = `

## ${header}

- ${x}

`;

Browser Support

Template Strings is an ES6 feature.

ES6 is fully supported in all modern browsers since June 2017:

Chrome 51Edge 15Firefox 54Safari 10Opera 38
May 2016Apr 2017Jun 2017Sep 2016Jun 2016

Complete String Reference > For a complete String reference, go to our: > Complete JavaScript String Reference. > The reference contains descriptions and examples of all string properties and methods.


Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let text = `Hello World!`;

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let text = `He's often called "Johnny"`;

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let text =

`The quick

brown fox

jumps over

the lazy dog`;

Syntax

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
${...}

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let firstName = "John";

let lastName = "Doe";



let text = `Welcome ${firstName}, ${lastName}!`;

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let price = 10;

let VAT = 0.25;



let total = `Total: ${(price * (1 + VAT)).toFixed(2)}`;