Tutorial

Tagged Template Literals in JavaScript (ES6 / ES2015)

Published on February 7, 2017
author

Alligator.io

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel