bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Working with Data
JavaScript•Working with Data

JavaScript String Templates

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JavaScript String Templates?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ text = `Hello World!`;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Beloved child has many names
Expression Substitution
Quotes Inside 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

let text = `Hello World!`;

Quotes Inside Strings

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

Example

let text = `He's often called "Johnny"`;

Multiline Strings

Example

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

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

Expression Substitution

Template Strings allow interpolation of expressions in strings:

Example

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

HTML Templates

Example

let header = "Template Strings";
let tags = ["template strings", "javascript", "es6"];
let html = `<h2>${header}</h2><ul>`;
for (const x of tags) {
  html += `<li>${x}</li>`;
}
html += `</ul>`;

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

Previous

JavaScript RegExp

Next

JavaScript Number Methods