Concatenating strings with template literals. Tagged templates

JavaScript

In this article, we cover ways to join strings. Even though it includes old methods of concatenating strings, we focus on template literals. Here you can learn how they work and how to expand their functionality with tags. Let’s go!

Before template literals

Back in the days, we didn’t have template literals and to merge strings we used other techniques. Some of them were:

Addition operator

As mentioned in the Looking into assembly code of coercion, if any operands of the + operator is a string, the result will also be a string. Such operation converts the other operand to a string if needed.

Since the string is a primitive type, it is immutable. It means that it can’t be altered and when attempting to add one string to the other, a new one is created. This applies to all of the ways of concatenating strings.

Trying to modify an existing string will not have any effect and throw an error in the strict mode!

Uncaught TypeError: Cannot assign to read only property ‘0’ of string ‘Hello’

Object descriptors ensure that – all characters in the string array are read-only.

Check out Object property descriptors, if you would like to know more about them

String.prototype.concat

The concat function joins together the calling string with provided arguments and returns a new string. An important thing is that it does not change the string, but returns a new one.

According to the MDN, using the addition operator is much faster.

Template literals

The template literals, previously also called template strings, are a new way to join strings introduced in ES6. Such strings are enclosed by the back-tick – ``  – the grave accent. Template literals can evaluate expressions inserted as a part of the string, enclosed in a dollar sign and curly brackets.

It looks clean and straightforward, especially combined with the usage of good variable names.

If you ever need to put a back-tick into the string, you need to escape it: to do this, put a backslash before the backtick.

A noteworthy aspect of using template literals are multi-line strings. If you press enter in the middle of a template literal, you insert a newline character.

To achieve the same output without template strings, you need to insert newline characters manually:

Hitting enter in a regular string causes Uncaught SyntaxError: Invalid or unexpected token and to prevent it, you need to escape newlines.

This does not insert newline characters though and you still need to add it manually.

Tagged templates

When you use regular template literals, your input is passed to a default function that concatenates it into a single string. An interesting thing is that you can change it by preceding the template literal with your function name that acts as a tag. By doing it, you create a tagged template.

Using tags is a more advanced way to put the template literals to work: with them, you can manipulate your strings before outputting them.

The first argument of the tag function is an array of string values from your literal. The rest of the arguments are the expressions. The return of the tag function is the output of the tagged template literal.

Imagine wanting to bold every expression that is a part of your template literal when using markdown. To bold something in markdown, you need to enclose it in asterisks.

A common use-case is in a popular library styled-components. It is a good example that tag functions don’t need to return a string:

In this case the  styled.div returns an object and your tag functions can also do that!

Summary

In this article, we covered string concatenation. It included going through the old ways of joining strings like the addition operator and functions like String.prototype.concat. We covered template strings, a new way of concatenating strings that came with the ES6. We learned about how they work and how to enhance their functionalities with tags.

Leave a Reply

Your email address will not be published. Required fields are marked *