Template Literals

Template strings (also called "template string") are string literals that allow embedded expressions. Template literals are enclosed by back-tick characters (``) instead of double or single quotes.

let testString = `Hello World`;

alert(testString);

 

Template literal support string interpolation, which means that they can contain embedded expressions. To embed an expression, place it inside a pair of {} brackets and preceed it with a $ character.

let name = "Mary";
let testString = `Hello ${name}`;

alert(testString);

 

A template literal expression can be any valid Javascript expression. For example:

let number = 3;
let testString = `3 squared = ${number * number} and 2 + 5 = ${2 + 5}`;

alert(testString);

 

Template literals can span overseveral lines.

let forenames = ["Anne", "Bob", "Cathy"];
let surnames = ["Allen", "Brown", "Carpenter"];
let htmlString = `
ForenameSurname
${forenames[0]}${surnames[0]}
${forenames[1]}${surnames[1]}
${forenames[2]}${surnames[2]}
`; alert(htmlString);

Write code to display the JSON object below as a list, as shown here. Do not use a for-loop or any other loop.

let people = [{"name":"Anne", "age":20}, {"name":"Bob", "age":21}, {"name":"Cathy", "age":22}, {"name": "David", "age": 23}];

 
<div align="center"><a href="file:///C|/Users/Derek/Documents/Notes/versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>