Template Literals & String interpolation in JavaScript

Template literals (or Template strings) in JavaScript are multi-line string literals that allow string interpolation. String interpolation allows us to include embedded expressions as part of the string. You can create multi-line strings, basic string formatting & tagged templates with Template strings. They are part of the ES2016/ES6 specification. Template strings are also called template literals. In this tutorial, we will learn about Template strings (or Literal) how to use it in Multiline Strings & string interpolation. We will also learn how to nest expressions and how to escape template literals using the backslash (\)

What is Template Strings

Template literals are literals delimited with backticks ` instead of single quote or double quotes.

The following is a example of template string.

What makes them powerful is you can include a embedded expression within them. We must include the expression inside a dollar sign and curly braces (${expression})

In the following example ${name} is an expression. The JavaScript evaluates it and replaces its value in the its original position.

Let us now explore the some of the use cases for the Template Strings

Multiline Strings

The following is an example of creating a multiline string. Just hit enter and continue in the next line. The \n character automatically gets inserted in the string.

Example

The multiline strings without template strings are created by inserting a \n newline character (LF).

As we said earlier, the /n (LF) character is inserted when you use the template strings. Hence the following code is true.

String Interpolation

string interpolation (expression Interpolation) is the process of evaluating a string literal containing one or more expressions. It evaluates the expression and coerces the result into a string. The result is then is replaced in the original string.

Example

In the following example, ${palyerName} is the expression. It is evaluated first and the resulting value Sachin Tendulkar is replaced at its place in the final string.

Multiple Expressions

Using Expressions

The following example shows that you can use any expressions inside the template string. It uses the arithmetic expression ${a+b}

The following example uses the ternary conditional operator(?) in the template string.

Nesting Expressions

You can nest expressions

Escaping in template strings

The backslash \ is used for escaping inside template literals. It enables you to include backticks and ${ inside template literals:

Tagged Templates

Another use of the Template string is in Tagged Templates. We tag a Template string to a function. This allows us to customize the parsing of the template string using the tagged function.

Reference

Template literals

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top