Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Strings Template

JavaScript Template Strings

  • String Templates
  • Template Strings
  • Template Literals

The cherished offspring is known by many titles.

Back-Tics Syntax

To define a string in Template Strings, back-ticks (“) are employed instead of quotes (“”).

Example

let text = `Hello World!`;

Quotes Inside Strings

Template Strings accommodate both single and double quotes within a string.

Example

let text = `He’s often called “Johnny”`;

Multiline Strings

Template Strings facilitate the creation of multi-line strings.

Example

let text =
`The quick
brown fox
jumps over
the lazy dog`
;

Interpolation

Template Strings offer a straightforward approach to embed variables and expressions into strings, known as string interpolation, utilizing the following syntax.

${...}

Variable Substitutions

Template Strings permit the inclusion of variables within strings.

Example

let firstName = “John”;
let lastName = “Doe”;

let text = `Welcome ${firstName}, ${lastName}!`;
The automatic substitution of variables with actual values is referred to as string interpolation.

Expression Substitution

Template Strings enable the incorporation of expressions within strings.

Example

let price = 10;
let VAT = 0.25;

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

String interpolation refers to the automatic substitution of expressions with actual values within interpolation strings.

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, introduced in ES6 (JavaScript 2015), have been fully supported in all modern browsers since June 2017.

IMG_4141

Template Strings lack support in Internet Explorer.