// Tutorial //

Tagged Template Literals in JavaScript (ES6 / ES2015)

Published on February 7, 2017
Default avatar
By Alligator.io
Developer and author at DigitalOcean.
Tagged Template Literals in JavaScript (ES6 / ES2015)

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

We briefly covered template literals, but they have an extra feature that we didn’t discuss: tags. You can tag your template literals with a function that will be called and can act as a kind of preprocessor on the content of the template literal.

Here’s a typical example of a template literal:

let name = 'Benedict';
let occupation = 'being awesome';

let sentence = `Hi! I'm ${ name } and I'm busy at ${ occupation }.`;

console.log(sentence);
// Hi! I'm Benedict and I'm busy at being awesome.

Now let’s tag the literal with a useless function:

function useless(strings, ...values) {
  return 'I render everything useless.';
}

let name = 'Benedict';
let occupation = 'being awesome';

let sentence = useless`Hi! I'm ${ name } and I'm busy at ${ occupation }.`;

console.log(sentence);
// I render everything useless.

Obviously the above doesn’t have any use, but where it starts to have more power is when we make use of the strings and values to construct the template literal with some processing.

With our current example:

  • strings is an array with 3 values: Hi! I’m , and I’m busy at and .
  • values is an array with the 2 interpolated values for the name and occupation variables. These values are passed as extra parameters to the tagged function, but here we make use of rest parameters to gather all the extra parameters into a values array.

There always is one more strings value then there are interpolated values. With our example, if there was not period at the end of then sentence, an empty string would be the last value for the strings array, to satisfy the fact that there needs to be one more string value.

Armed with that knowledge, we can therefore create a tag function for the string literal that actually does something:

function uppercase(strings, ...values) {
  let newStr = '';

  for (let i = 0; i < strings.length; i++) {
    if (i > 0) {
      newStr += values[i-1].toUpperCase();
    }
    newStr += strings[i];
  }

  return newStr;
}

let name = 'Benedict';
let occupation = 'being awesome';

let sentence = uppercase`Hi! I'm ${ name } and I'm busy at ${ occupation }.`;

console.log(sentence);
// Hi! I'm BENEDICT and I'm busy at BEING AWESOME.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!