Tagged Template Literals
In ES6 JavaScript added template literals which made working with strings so much easier. Writing multiline strings or strings with variables was something that used to be a massive pain, but template literals made those pains a thing of the past. Template literals aren’t only good for building out simple strings with variables, though. Tagged template literals can be used to make strings that run through custom functions in order to do some really cool and unique things. Before we talk about tagged template literals, though, we first need to understand how template literals work.
What Are Template Literals?
Template literals are a new way of defining strings by using backticks `
instead of single or double quotes.
Template literals also give you some extra powers when creating strings, such as the ability to put variables directly in the string without concatenation.
In order to add a variable inside a template literal the variable must be wrapped inside curly braces that start with a dollar sign ${}
. Everything in these curly braces will be executed as JavaScript code and the output will be put in the string at the location of the ${}
.
Template literals also give you the ability to define a string on multiple lines and keep all of the whitespace when printed.
One thing to watch out for with this approach, though, is that even the extra whitespace used at the beginning of lines is included in the output. This can lead to some confusing bugs with the output if you are not careful.
Lastly, template literals are useful when you want to use certain escape sequences such as new lines or tabs.
These features alone are enough to make template literals amazing, but on top of all this we also have tagged template literals which allow us to do some really neat things with template literals.
What Are Tagged Template Literals?
Tagged template literals are simply the ability to run a function on the content of a template literal. An example of an already existing tagged template literal is the String.raw
function. This tagged template literal will make it so that all escape sequences such as new lines are not computed and instead just printed as plain text.
Immediately, looking at this code looks weird. There are no parenthesis around the argument to String.raw
, but that is how tagged template literals work. A tagged template literal is called by putting the name of the function and then immediately following it with a template literal string. Let’s take a look at how we would do this with our own function.
As you can see a tagged template literal function is just a plain JavaScript function and whatever that function returns is what the tagged template literal will return. Our example is pretty useless since we are always returning the same string no matter what template literal is being used. In order to use the string passed to a tagged template literal we need to understand what arguments are passed to our tagged template literal function.
The first parameter to the function is an array of all the strings in the template literal separated by the ${}
expressions.
As you can see our strings array corresponds with the text before/after our variables and it even includes the blank string after the hobby
variable.
The rest of the parameters to the function correspond with each of the ${}
expressions in the order they appear in the template. This means if there are four ${}
expressions in the string then there will be four extra variables passed to the function, one for each variable.
Many times you will not know how many ${}
expressions a string will have, so you can use the rest operator to group all the ${}
expressions into one array. If you are unfamiliar with the rest operator I have a video on the topic you can find here.
With this syntax we now have all the information we need to reconstruct the string passed into the tagged template literal.
Now with all of this work we have effectively created a tagged template literal that returns the exact string we pass to it. This may seem like a lot of work to do essentially nothing, but we can do a ton of interesting things with these functions.
Bolding All Values With Tagged Template Literals
One thing we could do is bold all values from ${}
expressions since we have access to each value inside the values
array.
Removing Newlines From Multiline Strings
One thing that can be a bit annoying about template literals is that if you want to span multiple lines all of the new lines and tabs will be visible in the outputted string. We can create a simple tagged template literal that will remove all of these new line and tabs to create a single lined string which we can define across multiple lines.
Querying Elements With Tagged Template Literals
Tagged template literals can be used to return things other than strings too. For example, we could create a simple tagged template literal that will query all elements in a document based on the selector that is passed to the tagged template literal.
Last updated