Template Literals

Javascript 7 min min read Updated: Mar 07, 2026 Intermediate
Template Literals
Intermediate Topic 4 of 15

Template Literals in JavaScript

Template literals are a powerful feature introduced in modern JavaScript (ES6) that allow developers to create strings in a more flexible and readable way. They provide an easy method for embedding variables, expressions, and multi-line text directly inside a string.

Before template literals were introduced, developers often used string concatenation with the plus (+) operator to combine strings and variables. However, that approach could make the code difficult to read and maintain. Template literals simplify this process and make the code cleaner.

Template literals are enclosed using backticks (`) instead of single quotes or double quotes. This allows developers to insert variables and expressions inside the string using a special syntax called string interpolation.

Basic Syntax of Template Literals

Template literals use backticks instead of quotation marks. This allows variables to be inserted inside the string using the ${ } syntax.

javascript let name = "Rahul"; let message = `Hello ${name}, welcome to JavaScript`; console.log(message);

In this example, the variable name is inserted directly into the string using ${name}. This technique is known as string interpolation.

Why Template Literals are Useful

Template literals provide several advantages over traditional string concatenation.

  • Improves readability of code
  • Allows embedding variables easily
  • Supports multi-line strings
  • Allows expressions inside strings
  • Reduces the need for string concatenation

Because of these advantages, template literals are widely used in modern JavaScript applications.

String Concatenation vs Template Literals

Before ES6, developers combined strings and variables using the plus (+) operator.

javascript let name = "Amit"; let age = 25; let message = "My name is " + name + " and I am " + age + " years old."; console.log(message);

Using template literals, the same code becomes cleaner and easier to read.

javascript let name = "Amit"; let age = 25; let message = `My name is ${name} and I am ${age} years old.`; console.log(message);

This approach makes the string easier to understand and reduces code complexity.

Multi-Line Strings

One of the most useful features of template literals is the ability to create multi-line strings without using special characters like \n.

javascript let text = `JavaScript is a powerful programming language. It is widely used for web development. Template literals make working with strings easier.`; console.log(text);

This feature is especially helpful when writing HTML templates, email content, or formatted text.

Embedding Expressions

Template literals allow developers to insert JavaScript expressions inside strings. These expressions are evaluated before being inserted into the string.

javascript let a = 10; let b = 20; let result = `The sum of a and b is ${a + b}`; console.log(result);

The expression ${a + b} calculates the result and inserts the value into the string.

Using Template Literals with Functions

Template literals can also include function calls inside the interpolation syntax.

javascript function greet(name) { return `Hello ${name}`; } let message = `${greet("Student")}, welcome to JavaScript tutorial`; console.log(message);

This flexibility allows developers to dynamically generate strings using functions.

Using Template Literals for HTML Templates

Template literals are commonly used to generate HTML dynamically in JavaScript applications.

javascript let userName = "Priya"; let userAge = 22; let html = ` <div> <h2>User Information</h2> <p>Name: ${userName}</p> <p>Age: ${userAge}</p> </div> `; console.log(html);

This technique is widely used in modern frameworks like React, Vue, and Node.js applications.

Tagged Template Literals

JavaScript also supports a more advanced feature called tagged template literals. In this technique, a function processes the template literal before it is returned.

javascript function highlight(strings, name) { return `${strings[0]}${name}${strings[1]}`; } let result = highlight`Hello ${"JavaScript"} developers`; console.log(result);

This feature allows developers to customize how template strings are processed.

Advantages of Template Literals

Template literals provide several benefits when writing JavaScript code.

  • Cleaner and more readable code
  • Easy variable interpolation
  • Supports multi-line strings
  • Allows embedding expressions and functions
  • Useful for creating dynamic HTML content

Best Practices for Using Template Literals

When using template literals, developers should follow some best practices.

  • Use template literals instead of string concatenation
  • Keep expressions simple inside ${ }
  • Avoid overly complex template strings
  • Use template literals when generating HTML dynamically

Real-World Example

Template literals are commonly used in real applications such as displaying user information, generating UI components, and formatting dynamic messages.

javascript let product = "Laptop"; let price = 50000; let message = `The price of ${product} is ₹${price}.`; console.log(message);

This example dynamically creates a message using variables.

Conclusion

Template literals are an essential feature of modern JavaScript that make working with strings easier and more efficient. By using backticks and string interpolation, developers can write cleaner and more readable code.

Understanding template literals is important for building dynamic web applications, especially when working with APIs, generating HTML templates, or formatting output. Mastering this concept will help developers write modern and maintainable JavaScript programs.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators