-- Main.shssdhakchina - 26 Jan 2022

Template Literals

Template Literals simplifies many tasks such as string interpolation, evaluating expression inside a string.

Back-Tics Syntax

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

Example
let text = `Hello World!`;

Quotes Inside Strings

With template literals, you can use both single and double quotes inside a string:

Example
let text = `He's often called "Ramu"`;

Multiline Strings

Template literals allows multiline strings:

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

Interpolation

Template literals provide an easy way to interpolate variables and expressions into strings.

The method is called string interpolation.

The syntax is:

${...}

Variable Substitutions

Template literals allow variables in strings:

Example
let firstName = "Ramesh";
let lastName = "Kumar";

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

Expression Substitution

Template literals allow expressions in strings:

Example
let price = 10;
let VAT = 0.25;

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

HTML Templates

Example
let header = "Templates Literals";
let tags = ["template literals", "javascript", "es6"];

let html = `<h2>${header}</h2><ul>`;
for (const x of tags) {
  html += `<li>${x}</li>`;
}

html += `</ul>`;
Topic revision: r1 - 26 Jan 2022, shss\dhakchina
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback