I have the following code for Ecma-Script-6 template literals
let person = {name: 'John Smith'};
let tpl = `My name is ${person.name}.`;
let MyVar="My name is "+ person.name+".";
console.log("template literal= "+tpl);
console.log("my variable = "+MyVar);
The output is as follows:
template literal= My name is John Smith.
my variable = My name is John Smith.
this is the fiddle. I tried searching for the exact difference but couldn't find it, My question is what is the difference between these two statements,
let tpl = `My name is ${person.name}.`;
And
let MyVar = "My name is "+ person.name+".";
I am already able to get the string MyVar
concatenated with person.name
here, so what would be the scenario to use the template literal ?
It's a lot cleaner and as stated in the comments, is a common features in another languages. The other thing that I found nice was the line breaks, very useful when writing strings.
ES6
comes up with a new type of string literal, using the\
back-tick` as the delimiter. These literals do allow basic string interpolation expressions to be embedded, which are then automatically parsed and evaluated.As you can see, we used the ..`` around a series of characters, which are interpreted as a string literal, but any expressions of the form
${..}
are parsed and evaluated inline immediately.One really nice benefit of interpolated string literals is they are allowed to split across multiple lines:
Interpolated Expressions
Any valid expression is allowed to appear inside
${..}
in an interpolated stringlit‐ eral
, including function calls, inline function expression calls, and even otherinterpo‐ lated string literals
!Here, the inner \${who}s`` interpolated string literal was a little bit nicer convenience for us when combining the who variable with the
"s"
string, as opposed to who + "s". Also to keep an note is an interpolated string literal is justlexically scoped
where it appears, notdynamically scoped
in any wayUsing the
template literal
for the HTML is definitely more readable by reducing the annoyance.The plain old way:
With
ES6
:Tagged Template Literals
We can also tag a
template
string, when atemplate
string is tagged, theliterals
and substitutions are passed to function which returns the resulting value.We can use the
spread
operator here to pass multiple values. The first argument — we called it strings — is an array of all the plain strings (the stuff between any interpolated expressions).we then gather up all subsequent arguments into an array called values using the
... gather/rest operator
, though you could of course have left them as individual named parameters following the strings parameter like we did above(value1, value2 etc)
.The
argument(s)
gathered into our values array are the results of the already evaluated interpolation expressions found in the string literal. Atagged string literal
is like a processing step after the interpolations are evaluated but before the final string value is compiled, allowing you more control over generating the string from the literal. Let's look at an example of creating are-usable templates
.Raw Strings
our tag functions receive a first argument we called
strings
, which is anarray
. But there’s an additional bit of data included: the raw unprocessed versions of all the strings. You can access those raw string values using the.raw
property, like this:As you can see, the
raw
version of the string preserves the escaped \n sequence, while the processed version of the string treats it like an unescaped real new-line.ES6
comes with a built-in function that can be used as a string literal tag:String.raw(..)
. It simply passes through the raw versions of thestrings
:If you are using template literals only with placeholders (e.g.
${expression}
) like in the question's example, then the result is the same as just concatenating strings. Subjectively it looks better and is easier to read, especially for multi-line strings or strings containing both'
and"
since you don't have to escape those characters any more.Readability is a great feature, but the most interesting thing about templates are Tagged template literals:
In the third line of this example, a function named
tag
is called. The content of the template string is split into multiple variables, that you can access in the arguments of thetag
function: literal sections (in this exampleMy name is
and.
) and substitutions (John Smith
). The template literal will be evaluated to whatever thetag
function returns.The ECMAScript wiki lists some use cases, like automatically escaping or encoding input, or localization. You could create a tag function named
msg
that looks up the literal parts likeMy name is
and substitutes them with translations into the current locale's language, for example into German:The value returned by the tag function doesn't even have to be a string. You could create a tag function named
$
which evaluates the string and uses it as a query selector to return a collection of DOM nodes, like in this example:While, my answer does not directly address the question. I thought it may be of some interest to point out one drawback of using template literals in favor of array join.
Lets say I have
So some patients have a middleName and others do not.
If I wanted a string representing the full name of a patient
Then this would become "John undefined Smith"
However if I did
Then this would become just "John Smith"
EDIT
General_Twyckenham pointed out that a join on " " would result in an extra space between "John" and "Smith".
To get around this you can have a filter before the join to get rid of falsy values:
[patient1.firstName, patient1.middleName, patient1.lastName].filter(el => el).join(" ");