Ever wondered if there was a better way to concatenate string in JavaScript than to use + everywhere?
Or a better way to create multiline string than to use \n everywhere?
Well, there is!
What are Template Literals
Template literals is a “new” syntax to the JavaScript standard, which came in ES6 (ES2015).
They can do the same thing as the normal JavaScript string literals, but they have a couple of new possibilities, which makes them Awesome!
The syntactical difference is that instead of single or double quotes, you use “back-tics”: `
This alone doesn’t really bring any benefits though, so let’s take a look at what really makes these template literals so great!
String concatenation
String concatenation sucks with normal string literals.
Ever done something like this?
This was just one variable, and it is still a bit ugly and hard to read.
The new template literals are great for string concatenation with variables. Let’s just see the same example with template literals:
This is better! Even if you don’t know the syntax, you know what is going to happen here. And it scales a lot better too!
The ${username} is a template placeholder, and will be replaced at run-time with the proper variable.
You can also do your JavaScript expressions inside of the template placeholders, so you could do stuff like this:
Multiline strings
Have you ever had to construct a big multiline string with JavaScript?
Did it look like this?
Or perhaps you wanted to make it more readable by doing something like this?
Luckily, there is a better solution with template literals (or this part of the article would be pretty useless, right?).
You can just do it in the most natural way imaginable:
Now that is much easier to read. No more ugly escape character or string concatenation to create your multiline string variables.
More functionality
There is actually even more advanced functionality to template literals, but I believe most people won’t be needing them on a regular basis.
This functionality is the ability to create your own special tag functions to which the template literal is sent and processed, instead of just being concatenated.
Just to show you very briefly how this might look, take a look at the following simple example:
You can return whatever you want from the tag function actually; functions, objects, strings. They are very powerful, but probably not something you need for every day use. Good to know though!
If you want to read a little bit more about tagged template literals, take a look at the developer docs on MDN: Tagged Template Literals
Summary
In this post, I have shown why template literals are a great tool for us JavaScript developers. They are powerful, practical and easy to use!
They allow us to easily both concatenate and create multiline strings.
Follow me on Twitter: @gjermundbjaanes