codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

50 / 65 topics
50JavaScript Template Literals51JavaScript Destructuring52JavaScript Spread & Rest Operators53JavaScript Default Parameters54JavaScript Modules (import/export)55JavaScript Iterators and Generators56JavaScript Proxies
Tutorials/JavaScript/JavaScript Template Literals
🌐JavaScript

JavaScript Template Literals

Updated 2026-05-12
15 min read

JavaScript Template Literals

In the world of JavaScript, one of the most powerful features introduced in ES6 is the template literal. These literals provide a new way to work with strings, offering both string interpolation and multi-line support. Understanding template literals is crucial for writing clean, readable, and efficient code.

Introduction

Template literals are enclosed by backticks (`) instead of single or double quotes. They allow you to embed expressions directly inside strings using the \${expression} syntax. This feature simplifies the process of creating dynamic strings, making your code more concise and maintainable.

Additionally, template literals support multi-line strings without needing escape sequences like \n or \t. This makes them particularly useful for formatting complex strings, such as HTML templates or SQL queries.

Core Content

String Interpolation

String interpolation is the process of embedding expressions inside string literals. This allows you to dynamically insert variables and other expressions into a string.

Basic Example

Let's start with a simple example where we interpolate a variable into a string:

interpolate.js
1const name = "Alice";
2const greeting = `Hello, ${name}!`;
3console.log(greeting);
Output
Hello, Alice!

In this example, the variable name is embedded inside the template literal using \${name}. The resulting string is then logged to the console.

Expression Interpolation

You can also embed expressions directly into template literals. These expressions are evaluated at runtime and their results are inserted into the string.

expression.js
1const a = 5;
2const b = 10;
3const sum = `The sum of ${a} and ${b} is ${a + b}`;
4console.log(sum);
Output
The sum of 5 and 10 is 15

Here, the expression a + b is evaluated to 15, which is then inserted into the string.

Multi-Line Strings

Template literals also support multi-line strings without needing escape sequences. This makes them ideal for creating formatted text or HTML templates.

Basic Example

Let's create a multi-line string using template literals:

multiline.js
1const message = `This is a
2multi-line string.`;
3console.log(message);
Output
This is a
multi-line string.

In this example, the string message spans multiple lines within the template literal. The output preserves these line breaks.

HTML Templates

Template literals are particularly useful for creating HTML templates in JavaScript. Here's an example:

html-template.js
1const name = "Bob";
2const age = 25;
3const html = `
4<div>
5 <h1>${name}</h1>
6 <p>Age: ${age}</p>
7</div>
8`;
9console.log(html);
Output
<div>
  <h1>Bob</h1>
  <p>Age: 25</p>
</div>

In this example, the HTML template is created using a template literal. The variables name and age are embedded into the HTML structure.

Tagged Templates

Template literals can also be used with tagged functions. A tagged function is a function that takes a template literal as its first argument and an array of values as its second argument. This feature allows for advanced string manipulation.

Basic Example

Let's create a simple tagged function to format a greeting:

tagged-template.js
1function greet(strings, name) {
2return strings[0] + name.toUpperCase() + strings[1];
3}
4
5const message = greet`Hello, ${"Alice"}! How are you?`;
6console.log(message);
Output
Hello, ALICE! How are you?

In this example, the greet function is a tagged template that converts the name to uppercase. The strings array contains the static parts of the template literal ("Hello, " and "! How are you?"), while name contains the interpolated value.

Practical Example

Let's create a practical example where we use template literals to generate an HTML report for a user:

report.js
1const users = [
2{ name: "Alice", age: 28, email: "alice@example.com" },
3{ name: "Bob", age: 34, email: "bob@example.com" }
4];
5
6function generateReport(users) {
7return `
8 <h1>User Report</h1>
9 <ul>
10 ${users.map(user => `
11 <li>
12 <strong>Name:</strong> ${user.name}<br/>
13 <strong>Age:</strong> ${user.age}<br/>
14 <strong>Email:</strong> ${user.email}
15 </li>`).join('')}
16 </ul>
17`;
18}
19
20console.log(generateReport(users));
Output
<h1>User Report</h1>
  <ul>
    
      <li>
        <strong>Name:</strong> Alice<br/>
        <strong>Age:</strong> 28<br/>
        <strong>Email:</strong> alice@example.com
      </li>
    
      <li>
        <strong>Name:</strong> Bob<br/>
        <strong>Age:</strong> 34<br/>
        <strong>Email:</strong> bob@example.com
      </li>
    
  </ul>

In this example, the generateReport function takes an array of user objects and generates an HTML report using template literals. The map function is used to iterate over each user and create a list item for each one.

Summary

FeatureDescription
String InterpolationEmbed expressions directly inside strings using \${expression}.
Multi-Line StringsCreate multi-line strings without escape sequences.
Tagged TemplatesUse functions to manipulate template literals for advanced string processing.

What's Next?

In the next topic, we'll explore JavaScript Destructuring, a powerful feature that allows you to extract values from arrays and objects into variables. This will help simplify your code and make it more readable.

Stay tuned!


PreviousJavaScript Event LoopNext JavaScript Destructuring

Recommended Gear

JavaScript Event LoopJavaScript Destructuring