Multi line strings without template literals
In earlier times, it was possible to store multi line strings in comments within a function and then extract them from the function body:
function hereDoc(text) { // http://stackoverflow.com/a/5571069/180275
return text.toString().
replace(/^[^\/]+\/\*!?/, '').
replace(/\*\/[^\/]+$/, '');
}
multilineString = hereDoc(function() {/*! 1st line
3rd line
5th line
7ht line*/}
);
console.log(multilineString);
Placeholders
A template literal can contain placeholders (${…}
) whose expression is evaluated and embedded where they occur.
function twice(num) {
return num*2;
}
let num=42;
let greeting='Hello'
let expandedString = `${greeting}, the number is ${num}, twice the number is ${twice(num)}`;
console.log(expandedString);