Difference between Template literals and Tagged template literals?
ES6 has two new kinds of literals:
- template literals
- tagged template literals.
Template literals: multi-line string literals that support interpolation.
eg:
const firstName = 'Jane';
console.log(`Hello ${firstName}! How are you today?`);
Tagged template literals : are function calls whose parameters are provided via template literals.
eg:
String.raw`Hello ${firstName}! How are you today?
What is difference between these two literals ? and when to use Tagged template literals?
javascript ecmascript-6
add a comment |
ES6 has two new kinds of literals:
- template literals
- tagged template literals.
Template literals: multi-line string literals that support interpolation.
eg:
const firstName = 'Jane';
console.log(`Hello ${firstName}! How are you today?`);
Tagged template literals : are function calls whose parameters are provided via template literals.
eg:
String.raw`Hello ${firstName}! How are you today?
What is difference between these two literals ? and when to use Tagged template literals?
javascript ecmascript-6
Are you specifically asking aboutString.raw
or tagged template literals in general?
– Felix Kling
Mar 14 '17 at 7:19
@FelixKling i am asking about tagged template literals.
– Mukund Kumar
Mar 14 '17 at 7:21
3
Does ES6 tagged templates practical usability answer your question? The difference between a template literal and a tagged template literal is that the latter allows you to apply custom logic to template literals (instead of the default, which is string concatenation).
– Felix Kling
Mar 14 '17 at 7:23
add a comment |
ES6 has two new kinds of literals:
- template literals
- tagged template literals.
Template literals: multi-line string literals that support interpolation.
eg:
const firstName = 'Jane';
console.log(`Hello ${firstName}! How are you today?`);
Tagged template literals : are function calls whose parameters are provided via template literals.
eg:
String.raw`Hello ${firstName}! How are you today?
What is difference between these two literals ? and when to use Tagged template literals?
javascript ecmascript-6
ES6 has two new kinds of literals:
- template literals
- tagged template literals.
Template literals: multi-line string literals that support interpolation.
eg:
const firstName = 'Jane';
console.log(`Hello ${firstName}! How are you today?`);
Tagged template literals : are function calls whose parameters are provided via template literals.
eg:
String.raw`Hello ${firstName}! How are you today?
What is difference between these two literals ? and when to use Tagged template literals?
javascript ecmascript-6
javascript ecmascript-6
asked Mar 14 '17 at 7:15
Mukund KumarMukund Kumar
6,09643558
6,09643558
Are you specifically asking aboutString.raw
or tagged template literals in general?
– Felix Kling
Mar 14 '17 at 7:19
@FelixKling i am asking about tagged template literals.
– Mukund Kumar
Mar 14 '17 at 7:21
3
Does ES6 tagged templates practical usability answer your question? The difference between a template literal and a tagged template literal is that the latter allows you to apply custom logic to template literals (instead of the default, which is string concatenation).
– Felix Kling
Mar 14 '17 at 7:23
add a comment |
Are you specifically asking aboutString.raw
or tagged template literals in general?
– Felix Kling
Mar 14 '17 at 7:19
@FelixKling i am asking about tagged template literals.
– Mukund Kumar
Mar 14 '17 at 7:21
3
Does ES6 tagged templates practical usability answer your question? The difference between a template literal and a tagged template literal is that the latter allows you to apply custom logic to template literals (instead of the default, which is string concatenation).
– Felix Kling
Mar 14 '17 at 7:23
Are you specifically asking about
String.raw
or tagged template literals in general?– Felix Kling
Mar 14 '17 at 7:19
Are you specifically asking about
String.raw
or tagged template literals in general?– Felix Kling
Mar 14 '17 at 7:19
@FelixKling i am asking about tagged template literals.
– Mukund Kumar
Mar 14 '17 at 7:21
@FelixKling i am asking about tagged template literals.
– Mukund Kumar
Mar 14 '17 at 7:21
3
3
Does ES6 tagged templates practical usability answer your question? The difference between a template literal and a tagged template literal is that the latter allows you to apply custom logic to template literals (instead of the default, which is string concatenation).
– Felix Kling
Mar 14 '17 at 7:23
Does ES6 tagged templates practical usability answer your question? The difference between a template literal and a tagged template literal is that the latter allows you to apply custom logic to template literals (instead of the default, which is string concatenation).
– Felix Kling
Mar 14 '17 at 7:23
add a comment |
3 Answers
3
active
oldest
votes
With tagged template literal we able to modify the output of template literals using a function. The first argument contains an array of string literals. The second, and each argument after the first one, are the values of the processed substitution expressions. We can use any name to our function.
var a = 1;
var b = 2;
function tag(strings, ...values) {
console.log(strings[0]); // "One "
console.log(strings[1]); // " Two"
console.log(strings[2]); // " Three"
console.log(values[0]); // 1
console.log(values[1]); // 2
}
tag`One ${ a } Two ${ b } Three`;
// One
// Two
// Three
// 1
// 2
here our our tag function will return the output with custom formats
5
"first argument contains an array of string literals" nitpick: It contains an array of string values. A literal is a syntactic construct that is only interesting to the parser. The interpreter creates string values from them.
– Felix Kling
Mar 14 '17 at 7:29
add a comment |
ES6 has new features
Template literals
and
Tagged template literals (Tagged templates)
which make working with strings easier. You wrap your text in `backticks`
With this we can:
1.Interpolate variables
let foo = "abc";
console.log(`Welcome ${foo}`); // Welcome abc
2.Interpolate any kind of expression
console.log(`2+3 = ${2+3}`) // 2+3 = 5
3.Declare strings with both ' and " quotation marks without having to
escape anything.
let foo = `foo is 'bar', "bar" is foo`
console.log(foo); // "foo is 'bar', "bar" is foo"
4.Cleaner syntax for multi-line string
let text = `foo is bar
bar is foo`
console.log(text);
//"foo is bar
//bar is foo"
5.Tagged templates, we can pass template literals to a function, here is how:
let person = 'Mike';
let age = 28;
let output = myTag `that ${ person } is ${ age }`;
function myTag(strings, personExp, ageExp) {
//strings[0] gets value "that "
//strings[1] gets value " is "
//personExp gets value " Mike "
//ageStr gets value "28"
return strings[0] + personExp + strings[1] + ageExp;
}
console.log(output);
// that Mike is 28
6.String.raw, we can get the raw form, here is the example:
let text = String.raw `The "n" newline won't result in a new line.'
console.log(text);
// The "n" newline won't result in a new line.
Hope this helps!!!!!!
add a comment |
The best way to process the input from a tagged template literal function was pointed out to me by this tutorial that elegantly uses the ternary operator to solve the problem. Just as a side note. There are always index + 1 "values" than there are "string" parts. This can be confusing at first.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42779840%2fdifference-between-template-literals-and-tagged-template-literals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
With tagged template literal we able to modify the output of template literals using a function. The first argument contains an array of string literals. The second, and each argument after the first one, are the values of the processed substitution expressions. We can use any name to our function.
var a = 1;
var b = 2;
function tag(strings, ...values) {
console.log(strings[0]); // "One "
console.log(strings[1]); // " Two"
console.log(strings[2]); // " Three"
console.log(values[0]); // 1
console.log(values[1]); // 2
}
tag`One ${ a } Two ${ b } Three`;
// One
// Two
// Three
// 1
// 2
here our our tag function will return the output with custom formats
5
"first argument contains an array of string literals" nitpick: It contains an array of string values. A literal is a syntactic construct that is only interesting to the parser. The interpreter creates string values from them.
– Felix Kling
Mar 14 '17 at 7:29
add a comment |
With tagged template literal we able to modify the output of template literals using a function. The first argument contains an array of string literals. The second, and each argument after the first one, are the values of the processed substitution expressions. We can use any name to our function.
var a = 1;
var b = 2;
function tag(strings, ...values) {
console.log(strings[0]); // "One "
console.log(strings[1]); // " Two"
console.log(strings[2]); // " Three"
console.log(values[0]); // 1
console.log(values[1]); // 2
}
tag`One ${ a } Two ${ b } Three`;
// One
// Two
// Three
// 1
// 2
here our our tag function will return the output with custom formats
5
"first argument contains an array of string literals" nitpick: It contains an array of string values. A literal is a syntactic construct that is only interesting to the parser. The interpreter creates string values from them.
– Felix Kling
Mar 14 '17 at 7:29
add a comment |
With tagged template literal we able to modify the output of template literals using a function. The first argument contains an array of string literals. The second, and each argument after the first one, are the values of the processed substitution expressions. We can use any name to our function.
var a = 1;
var b = 2;
function tag(strings, ...values) {
console.log(strings[0]); // "One "
console.log(strings[1]); // " Two"
console.log(strings[2]); // " Three"
console.log(values[0]); // 1
console.log(values[1]); // 2
}
tag`One ${ a } Two ${ b } Three`;
// One
// Two
// Three
// 1
// 2
here our our tag function will return the output with custom formats
With tagged template literal we able to modify the output of template literals using a function. The first argument contains an array of string literals. The second, and each argument after the first one, are the values of the processed substitution expressions. We can use any name to our function.
var a = 1;
var b = 2;
function tag(strings, ...values) {
console.log(strings[0]); // "One "
console.log(strings[1]); // " Two"
console.log(strings[2]); // " Three"
console.log(values[0]); // 1
console.log(values[1]); // 2
}
tag`One ${ a } Two ${ b } Three`;
// One
// Two
// Three
// 1
// 2
here our our tag function will return the output with custom formats
edited Mar 14 '17 at 7:30
answered Mar 14 '17 at 7:27
Pandiyan CoolPandiyan Cool
4,05543364
4,05543364
5
"first argument contains an array of string literals" nitpick: It contains an array of string values. A literal is a syntactic construct that is only interesting to the parser. The interpreter creates string values from them.
– Felix Kling
Mar 14 '17 at 7:29
add a comment |
5
"first argument contains an array of string literals" nitpick: It contains an array of string values. A literal is a syntactic construct that is only interesting to the parser. The interpreter creates string values from them.
– Felix Kling
Mar 14 '17 at 7:29
5
5
"first argument contains an array of string literals" nitpick: It contains an array of string values. A literal is a syntactic construct that is only interesting to the parser. The interpreter creates string values from them.
– Felix Kling
Mar 14 '17 at 7:29
"first argument contains an array of string literals" nitpick: It contains an array of string values. A literal is a syntactic construct that is only interesting to the parser. The interpreter creates string values from them.
– Felix Kling
Mar 14 '17 at 7:29
add a comment |
ES6 has new features
Template literals
and
Tagged template literals (Tagged templates)
which make working with strings easier. You wrap your text in `backticks`
With this we can:
1.Interpolate variables
let foo = "abc";
console.log(`Welcome ${foo}`); // Welcome abc
2.Interpolate any kind of expression
console.log(`2+3 = ${2+3}`) // 2+3 = 5
3.Declare strings with both ' and " quotation marks without having to
escape anything.
let foo = `foo is 'bar', "bar" is foo`
console.log(foo); // "foo is 'bar', "bar" is foo"
4.Cleaner syntax for multi-line string
let text = `foo is bar
bar is foo`
console.log(text);
//"foo is bar
//bar is foo"
5.Tagged templates, we can pass template literals to a function, here is how:
let person = 'Mike';
let age = 28;
let output = myTag `that ${ person } is ${ age }`;
function myTag(strings, personExp, ageExp) {
//strings[0] gets value "that "
//strings[1] gets value " is "
//personExp gets value " Mike "
//ageStr gets value "28"
return strings[0] + personExp + strings[1] + ageExp;
}
console.log(output);
// that Mike is 28
6.String.raw, we can get the raw form, here is the example:
let text = String.raw `The "n" newline won't result in a new line.'
console.log(text);
// The "n" newline won't result in a new line.
Hope this helps!!!!!!
add a comment |
ES6 has new features
Template literals
and
Tagged template literals (Tagged templates)
which make working with strings easier. You wrap your text in `backticks`
With this we can:
1.Interpolate variables
let foo = "abc";
console.log(`Welcome ${foo}`); // Welcome abc
2.Interpolate any kind of expression
console.log(`2+3 = ${2+3}`) // 2+3 = 5
3.Declare strings with both ' and " quotation marks without having to
escape anything.
let foo = `foo is 'bar', "bar" is foo`
console.log(foo); // "foo is 'bar', "bar" is foo"
4.Cleaner syntax for multi-line string
let text = `foo is bar
bar is foo`
console.log(text);
//"foo is bar
//bar is foo"
5.Tagged templates, we can pass template literals to a function, here is how:
let person = 'Mike';
let age = 28;
let output = myTag `that ${ person } is ${ age }`;
function myTag(strings, personExp, ageExp) {
//strings[0] gets value "that "
//strings[1] gets value " is "
//personExp gets value " Mike "
//ageStr gets value "28"
return strings[0] + personExp + strings[1] + ageExp;
}
console.log(output);
// that Mike is 28
6.String.raw, we can get the raw form, here is the example:
let text = String.raw `The "n" newline won't result in a new line.'
console.log(text);
// The "n" newline won't result in a new line.
Hope this helps!!!!!!
add a comment |
ES6 has new features
Template literals
and
Tagged template literals (Tagged templates)
which make working with strings easier. You wrap your text in `backticks`
With this we can:
1.Interpolate variables
let foo = "abc";
console.log(`Welcome ${foo}`); // Welcome abc
2.Interpolate any kind of expression
console.log(`2+3 = ${2+3}`) // 2+3 = 5
3.Declare strings with both ' and " quotation marks without having to
escape anything.
let foo = `foo is 'bar', "bar" is foo`
console.log(foo); // "foo is 'bar', "bar" is foo"
4.Cleaner syntax for multi-line string
let text = `foo is bar
bar is foo`
console.log(text);
//"foo is bar
//bar is foo"
5.Tagged templates, we can pass template literals to a function, here is how:
let person = 'Mike';
let age = 28;
let output = myTag `that ${ person } is ${ age }`;
function myTag(strings, personExp, ageExp) {
//strings[0] gets value "that "
//strings[1] gets value " is "
//personExp gets value " Mike "
//ageStr gets value "28"
return strings[0] + personExp + strings[1] + ageExp;
}
console.log(output);
// that Mike is 28
6.String.raw, we can get the raw form, here is the example:
let text = String.raw `The "n" newline won't result in a new line.'
console.log(text);
// The "n" newline won't result in a new line.
Hope this helps!!!!!!
ES6 has new features
Template literals
and
Tagged template literals (Tagged templates)
which make working with strings easier. You wrap your text in `backticks`
With this we can:
1.Interpolate variables
let foo = "abc";
console.log(`Welcome ${foo}`); // Welcome abc
2.Interpolate any kind of expression
console.log(`2+3 = ${2+3}`) // 2+3 = 5
3.Declare strings with both ' and " quotation marks without having to
escape anything.
let foo = `foo is 'bar', "bar" is foo`
console.log(foo); // "foo is 'bar', "bar" is foo"
4.Cleaner syntax for multi-line string
let text = `foo is bar
bar is foo`
console.log(text);
//"foo is bar
//bar is foo"
5.Tagged templates, we can pass template literals to a function, here is how:
let person = 'Mike';
let age = 28;
let output = myTag `that ${ person } is ${ age }`;
function myTag(strings, personExp, ageExp) {
//strings[0] gets value "that "
//strings[1] gets value " is "
//personExp gets value " Mike "
//ageStr gets value "28"
return strings[0] + personExp + strings[1] + ageExp;
}
console.log(output);
// that Mike is 28
6.String.raw, we can get the raw form, here is the example:
let text = String.raw `The "n" newline won't result in a new line.'
console.log(text);
// The "n" newline won't result in a new line.
Hope this helps!!!!!!
edited Sep 3 '17 at 6:24
answered May 7 '17 at 11:01
Manishz90Manishz90
1,318811
1,318811
add a comment |
add a comment |
The best way to process the input from a tagged template literal function was pointed out to me by this tutorial that elegantly uses the ternary operator to solve the problem. Just as a side note. There are always index + 1 "values" than there are "string" parts. This can be confusing at first.
add a comment |
The best way to process the input from a tagged template literal function was pointed out to me by this tutorial that elegantly uses the ternary operator to solve the problem. Just as a side note. There are always index + 1 "values" than there are "string" parts. This can be confusing at first.
add a comment |
The best way to process the input from a tagged template literal function was pointed out to me by this tutorial that elegantly uses the ternary operator to solve the problem. Just as a side note. There are always index + 1 "values" than there are "string" parts. This can be confusing at first.
The best way to process the input from a tagged template literal function was pointed out to me by this tutorial that elegantly uses the ternary operator to solve the problem. Just as a side note. There are always index + 1 "values" than there are "string" parts. This can be confusing at first.
answered Sep 10 '18 at 6:36
InfiniteStackInfiniteStack
31916
31916
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42779840%2fdifference-between-template-literals-and-tagged-template-literals%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you specifically asking about
String.raw
or tagged template literals in general?– Felix Kling
Mar 14 '17 at 7:19
@FelixKling i am asking about tagged template literals.
– Mukund Kumar
Mar 14 '17 at 7:21
3
Does ES6 tagged templates practical usability answer your question? The difference between a template literal and a tagged template literal is that the latter allows you to apply custom logic to template literals (instead of the default, which is string concatenation).
– Felix Kling
Mar 14 '17 at 7:23