The cherished offspring is known by many titles.
To define a string in Template Strings, back-ticks (“) are employed instead of quotes (“”).
Example
let text = `Hello World!`; |
Template Strings accommodate both single and double quotes within a string.
Example
let text = `He’s often called “Johnny”`; |
Template Strings facilitate the creation of multi-line strings.
Example
let text = `The quick brown fox jumps over the lazy dog`; |
Template Strings offer a straightforward approach to embed variables and expressions into strings, known as string interpolation, utilizing the following syntax.
${...} |
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. |
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>`; |
Template Strings, introduced in ES6 (JavaScript 2015), have been fully supported in all modern browsers since June 2017.
Template Strings lack support in Internet Explorer.