Introduction

In this article, we are going to talk about JavaScript Template Literals. They were also called Template Strings prior to the ES2015 specification.

Other than that, we are also going to cover what Tagged Template Literals are and how we can use them with regular Template Literals.

What is a Template Literal?

Template Literals were introduced with JavaScript ES2015 (ES6) to handle strings in an easier and more readable way.

It allows us to embed expressions (expression interpolation) inside a string declaration, handle multiline strings and create “tagged template literals” which is a more advanced form of template literals.

The Old Way of Handling String Templates

Before ES6, single quotes or double quotes were used to declare a string. Consider the following example:

let x = 'This is a sample string';
let y = "This is a sample string with a 'quote' in it";
let z = 'This is a sample string with a "double quote" in it';

let a = 'This is another sample with a \'quote\' in it';
let b = "This is yet another sample with a \"double quote\" in it";

// a -> This is another sample with a 'quote' in it
// b -> This is yet another sample with a "double quote" in it

#node #javascript #es6

ES6 Template/String Literals in Node.js
4.95 GEEK