Prior to ES6, the old way to concatenate strings together was by using the string concatenation operator (+
).
const student = {
name: 'Richard Kalehoff',
guardian: 'Mr. Kalehoff'
};
const teacher = {
name: 'Mrs. Wilson',
room: 'N231'
}
let message = student.name + ' please see ' + teacher.name + ' in ' + teacher.room + ' to pick up your report card.';
Returns:Richard Kalehoff please see Mrs. Wilson in N231 to pick up your report card.
This works alright, but it gets more complicated when you need to build multi-line strings.
let note = teacher.name + ',\n\n' +
'Please excuse ' + student.name + '.\n' +
'He is recovering from the flu.\n\n' +
'Thank you,\n' +
student.guardian;
Returns:
Mrs. Wilson,Please excuse Richard Kalehoff.
He is recovering from the flu.Thank you,
Mr. Kalehoff
However, that’s changed with the introduction of template literals(previously referred to as "template strings" in development releases of ES6).
NOTE: As an alternative to using the string concatenation operator (
+
), you can use the String'sconcat()
method, but both options are rather clunky for simulating true string interpolation.
Template Literals
Template literals are essentially string literals that include embedded expressions.
Denoted with backticks (```````\) instead of single quotes \(
''\) or double quotes \(
""\), template literals can contain placeholders which are represented using
${expression}`. This makes it_much easier_to build strings.
Here's the previous examples using template literals.
let message = `${student.name} please see ${teacher.name} in ${teacher.room} to pick up your report card.`;
Returns:Richard Kalehoff please see Mrs. Wilson in N231 to pick up your report card.
By using template literals, you can drop the quotes along with the string concatenation operator. Also, you can reference the object's properties inside expressions.
Here, you try. Change thegreeting
string below to use a template literal. Also, feel free to swap in your name for the placeholder.
/*
* Instructions: Change the `greeting` string to use a template literal.
*/
const myName = '[NAME]';
const greeting = 'Hello, my name is ' + myName;
console.log(greeting);
-> const greeting = `Hello, my name is ${myName}`;
...but what about the multi-line example from before?
let not = `${teacher.name},
Please excuse ${student.name}.
He is recovering from the flu.
Thank you,
${student.guardian}`;
Here’s where template literals really shine. In the animation above, the quotes and string concatenation operator have been dropped, as well as the newline characters (\n
). That's because template literals also preserve newlines as part of the string!
TIP:Embedded expressions inside template literals can do more than just reference variables. You can perform operations, call functions and use loops inside embedded expressions!
/*
* Programming Quiz: Build an HTML Fragment (1-2)
*/
const cheetah = {
name: 'Cheetah',
scientificName: 'Acinonyx jubatus',
lifespan: '10-12 years',
speed: '68-75 mph',
diet: 'carnivore',
summary: 'Fastest mammal on land, the cheetah can reach speeds of 60 or perhaps even 70 miles (97 or 113 kilometers) an hour over short distances. It usually chases its prey at only about half that speed, however. After a chase, a cheetah needs half an hour to catch its breath before it can eat.',
fact: 'Cheetahs have “tear marks” that run from the inside corners of their eyes down to the outside edges of their mouth.'
};
// creates an animal trading card
function createAnimalTradingCardHTML(animal) {
const cardHTML = `<div class="card">
<h3 class="name">${animal.name}</h3>
<img src="${animal.name}.jpg" alt="${animal.name}" class="picture">
<div class="description">
<p class="fact"> ${animal.fact} </p>
<ul class="details">
<li><span class="bold">Scientific Name</span>: ${animal.scientificName} </li>
<li><span class="bold">Average Lifespan</span>: ${animal.lifespan} </li>
<li><span class="bold">Average Speed</span>: ${animal.speed} </li>
<li><span class="bold">Diet</span>: ${animal.diet} </li>
</ul>
<p class="brief"> ${animal.summary} </p>
</div>
</div>`;
return cardHTML;
}
console.log(createAnimalTradingCardHTML(cheetah));