Skip to main content

Command Palette

Search for a command to run...

Template Literals in JavaScript

Updated
4 min read
Template Literals in JavaScript

If you have started building projects in JavaScript , probably you have came across a frustrating point where handling strings becomes literally messy.

At first , it seems so simple -- just display a message, log the data, show some result of calculation. But the moment dynamic values enter the picture , the code starts getting messy. String breaks across lines, multiple stiches using + operator. It hampers readability a lot.

A simple feature seems to be very harder . Lets see a practical scenario to know how to improve this.


The Scenario : A Student Dashboard

Imagine you are building a student dashboard for let's say any cohort. When a user logs in , the app should display a clear message displaying the student's (user's) name , course and progress.

It's a very common real world situation , where string handling matters a lot.

Old Method: String Concatenation:

Just before ES6 the only approach available was :

const name = "Sanghita";
const course = "Web Dev Cohort 2026";
const progress = 70;

const message = "Hello " + name + ", welcome to " + course +
                ". Your progress is " + progress + "%.";

console.log(message);

It works well, but problem arises as soon as teh string grows. The structure feels broken. You have to constantly manage spaces, punctuation, and line breaks manually. And the more dynamic the content gets, the harder it becomes to maintain.

This approach doesn’t scale well in real applications.


The Better Way : Template Literals

In JavaScript a much more cleaner way to handle strings is Template Literals (` `).

const message = `Hello \({name}, welcome to \){course}. Your progress is ${progress}%.`;

In this case , the strings becomes like natural sentences ( Highly readable ). Ths small change from " " to ` ` improves both the readability and maintainability of code.


Understanding String Interpolation

The core Idea behind Template Literals is string interpolation - i.e. inserting variables directly in a string using ${}

const name = "Sanghita";
console.log(`Hello ${name}`); // Hello Sanghita

There is no need to manually join the string and the variable. It injects the variable , inside the string , making the code easier to understand at a glance.


Multiline Strings Without \n

When dealing with multi-line content , specially HTML.

Earlier, developers had to rely on concatenation or escape characters:

const html = "<div>" +
             "<h1>Hello " + name + "</h1>" +
             "<p>Progress: " + progress + "%</p>" +
             "</div>";

With template literals, this becomes much cleaner:

const html = `
  <div>
    <h1>Hello ${name}</h1>
    <p>Progress: ${progress}%</p>
  </div>
`;

Now the structure is clearly visible. No extra effort is needed to manage formatting — the code reflects the final output naturally.


Adding Logic inside the Strings

Template Literals also allow expressions, giving a room to embed logic directly inside a string

const status = progress > 80 ? "Excellent " : "Keep Going ";

const message = `Hello \({name}, your progress is \){progress}%.
Status: ${status}`;

This helps to get dynamic output without complicating the code .


Before VS After

The improvement might seem small at first, but it adds up quickly. It makes code less cluttered, readable, and also less prone to mistake.

Old approach:

const message = "Hello " + name + ", welcome!";

Modern approach:

const message = `Hello ${name}, welcome!`;

Where This Actually Helps

Template literals show their value the most in real-world application.

  1. Makes UI generation cleaner

    const card = `
      <div class="card">
        <h2>${name}</h2>
        <p>${course}</p>
      </div>
    `;
    
  2. Simplifies notifications

    const notification = `Congrats ${name}! You completed JavaScript Arrays.`;
    
  3. Improves debugging clarity

    console.log(`User \({name} has \){progress}% progress`);
    

Ultimately it improves readability & maintainability.

A Simple Way to Think About It

Instead of building strings piece by piece, think of writing them as complete sentences.

Then just insert the dynamic parts using ${}.

That’s it.