Skip to main content

Command Palette

Search for a command to run...

Template Literals in JavaScript: Writing Cleaner Strings Without the Mess

Updated
4 min read
Template Literals in JavaScript: Writing Cleaner Strings Without the Mess
S
A front-end developer who’s always learning, building projects, and writing blogs to simplify web concepts

When you start writing JavaScript, handling strings feels simple. But as soon as you try to combine text with variables or create longer messages, things can get messy quickly.

Many beginners struggle with readability when using traditional string concatenation. That’s where template literals come in. They solve a very real problem and make your code easier to write and understand.

In this article, we’ll walk through what template literals are, why they matter, and how they improve everyday JavaScript code.


The Problem with String Concatenation

Before template literals, combining strings and variables looked like this:

const name = "Aman";
const age = 22;

const message = "My name is " + name + " and I am " + age + " years old.";

This works, but it’s not very readable. As the sentence grows, the number of + operators increases, and the structure becomes harder to follow.

It gets worse with longer or formatted text:

const text = "JavaScript is widely used. " +
             "It powers websites. " +
             "It also runs on servers.";

You must manually handle spacing, line breaks, and operators. Small errors are easy to make, causing the code to lose its resemblance to the final output.


What Are Template Literals?

Template literals are a way to write strings using backticks (`) instead of quotes.

const message = `Hello, world!`;

They allow you to insert variables directly inside the string and write multi-line text naturally.


Embedding Variables (String Interpolation)

Instead of joining strings with +, template literals let you insert values using ${}.

const name = "Aman";
const age = 22;

const message = `My name is \({name} and I am \){age} years old.`;

This reads like a normal sentence. You don’t have to mentally process operators anymore.

You can also include expressions:

const price = 500;
const tax = 50;

const total = `Total: ${price + tax}`;

This reduces extra variables and keeps logic close to the output.


Multi-line Strings Made Simple

Template literals allow you to write text across multiple lines without breaking it.

const paragraph = `JavaScript is used for web development.
It helps create interactive websites.
It also works on the server side.`;

The formatting stays exactly as written. There is no need for \n a string joining.


Before vs After: A Clear Comparison

Let’s compare both approaches side by side.

Old way:

const firstName = "Riya";
const city = "Kolkata";

const message = "Hello " + firstName + ", you live in " + city + ".";

With template literals:

const message = `Hello \({firstName}, you live in \){city}.`;

Now for a multi-line example:

Old way:

const info = "Name: " + firstName + "\n" +
             "City: " + city + "\n" +
             "Status: Active";

With template literals:

const info = `Name: ${firstName}
City: ${city}
Status: Active`;

The second version is easier to read and closer to the final output.


Where You’ll Use Template Literals

Template literals are used in many everyday situations.

You’ll use them when displaying messages:

const user = "Sara";
const greeting = `Welcome back, ${user}!`;

When generating HTML:

const card = `
  <div class="card">
    <h2>${name}</h2>
    <p>${role}</p>
  </div>
`;

When logging useful information:

console.log(`User ${userId} clicked the button`);

And when returning formatted data from functions:

function getOrderSummary(item, quantity) {
  return `You ordered \({quantity} \){item}(s).`;
}

Why This Matters

Template literals improve clarity. Your strings look like the final output, which reduces confusion when reading code later.

They also reduce small errors like missing spaces or misplaced operators. Over time, this leads to cleaner and more maintainable code.


conslusion

Template literals are a simple feature with a big effect. They eliminate the hassle of joining strings and make your code clearer and easier to write. If you're learning JavaScript now, this should be your go-to method for handling strings. Once you're familiar with it, returning to the old method will seem pointless.

JavaScript Journey: From Basics to Core Concepts

Part 13 of 29

This series documents my journey of learning JavaScript and breaking down important concepts in a simple way. Each article covers a core JavaScript topic with clear explanations and beginner-friendly examples. From basic concepts to essential JavaScript features, the goal of this series is to make JavaScript easier to understand while practicing and sharing what I learn.

Up next

The new Keyword in JavaScript

When you're learning JavaScript, one of the first confusing things is how objects are actually created. And then you see this small keyword — new. It looks simple, but it does a lot behind the scenes.