Skip to main content

Command Palette

Search for a command to run...

Template Literals in JavaScript

Updated
9 min readView as Markdown
Template Literals in JavaScript

When we start learning JavaScript, one of the basic things we work with is strings.

A string is a collection of characters used to store text.

For example:

let name = "Tanishka";
let city = "Nashik";

Here, "Tanishka" and "Nashik" are strings.

While working with JavaScript, we often need to create messages by combining strings with variables. Earlier, we commonly used the + operator for this. It works, but longer messages can become difficult to read.

JavaScript provides a cleaner way to work with strings called Template Literals.

What are Template Literals?

Template literals are a way of creating strings using backticks instead of single or double quotes.

Normal string:

let message = "Hello World!";

Template literal:

let message = `Hello World!`;

The important thing here is the backtick:

`

Template literals allow us to:

  • Add variables directly inside strings

  • Write multi-line strings

  • Use expressions

  • Create dynamic content

  • Write cleaner code


1. Problems with Traditional String Concatenation

Before template literals, we commonly used string concatenation.

String concatenation means joining two or more strings using the + operator.

For example:

let name = "Tanishka";
let age = 20;
let city = "Nashik";

console.log("My name is " + name + ". I am " + age + " years old and I live in " + city + ".");

Output:

My name is Tanishka. I am 20 years old and I live in Nashik.

The output is correct, but the code contains many + operators and quotation marks.

As the sentence becomes longer, it becomes:

  • Difficult to read

  • Difficult to write

  • Easy to make mistakes in

  • Less clean

For example:

let product = "Laptop";
let price = 50000;
let quantity = 2;

console.log("You selected " + quantity + " " + product + "s. The total price is ₹" + (price * quantity) + ".");

This works, but there are many pieces that have to be joined together.

Template literals solve this problem.


2. Template Literal Syntax

The syntax is very simple.

Instead of:

"Hello World"

we use:

`Hello World`

For example:

let message = `Hello World!`;

console.log(message);

Output:

Hello World!

The main thing to remember is that template literals use backticks.

Their biggest advantage comes when we want to add variables.


3. Embedding Variables in Strings

One of the most useful features of template literals is string interpolation.

String interpolation means putting variables directly inside a string.

We use:

${variable}

For example:

let name = "Tanishka";

console.log(`Hello ${name}!`);

Output:

Hello Tanishka!

Here, ${name} is replaced by the value stored in the name variable.

Traditional method

let name = "Tanishka";

console.log("Hello " + name + "!");

Template literal method

let name = "Tanishka";

console.log(`Hello ${name}!`);

Both give the same output, but the second method is cleaner and easier to read.


4. Using Multiple Variables

We can add multiple variables inside the same template literal.

let name = "Tanishka";
let age = 20;
let city = "Nashik";

console.log(`My name is ${name}. I am ${age} years old and I live in ${city}.`);

Output:

My name is Tanishka. I am 20 years old and I live in Nashik.

We don't need multiple + operators. We can simply write the sentence and add variables wherever we need them.


5. Using Expressions

Template literals can also be used with JavaScript expressions.

For example:

let a = 10;
let b = 20;

console.log(`The sum of ${a} and ${b} is ${a + b}.`);

Output:

The sum of 10 and 20 is 30.

Here, ${a + b} is calculated by JavaScript before displaying the string.

Another example:

let price = 500;
let quantity = 3;

console.log(`The total price is ₹${price * quantity}.`);

Output:

The total price is ₹1500.

6. Multi-Line Strings

Another useful feature of template literals is that they support multi-line strings.

We can simply write:

let message = `Hello!
Welcome to my website.
Thank you for visiting.`;

console.log(message);

Output:

Hello!
Welcome to my website.
Thank you for visiting.

We can directly press Enter inside the template literal.

Before template literals, developers often used \n:

let message = "Hello!\nWelcome to my website.\nThank you for visiting.";

This works, but template literals make multi-line text easier to read.


7. Template Literals for HTML

Template literals are also useful when working with HTML through JavaScript.

For example, we can create a simple student card:

let name = "Tanishka";
let age = 20;

let card = `
<div>
    <h2>${name}</h2>
    <p>Age: ${age}</p>
</div>
`;

console.log(card);

Output:

<div>
    <h2>Tanishka</h2>
    <p>Age: 20</p>
</div>

This is useful when we need to create HTML content dynamically.


8. Before vs After Template Literals

Let's compare both methods.

String Concatenation

let name = "Tanishka";
let course = "Computer Engineering";
let year = 3;

console.log(
    "My name is " + name +
    ". I am studying " + course +
    " and I am in " + year + "rd year."
);

There are many + signs and the sentence is divided into several parts.

Template Literals

let name = "Tanishka";
let course = "Computer Engineering";
let year = 3;

console.log(
    `My name is ${name}. I am studying ${course} and I am in ${year}rd year.`
);

The second version is much easier to read.

We can almost read it like a normal sentence.


9. Why Template Literals Improve Readability

One of the biggest advantages of template literals is better readability.

Compare:

"Hello " + name + ", your score is " + score + " out of " + total;

with:

`Hello ${name}, your score is ${score} out of ${total}`;

The second version is easier to understand.

When we have many variables, this difference becomes even more noticeable.

Template literals let us focus on the actual sentence instead of worrying about where each + should go.


10. Common Use Cases

Template literals are commonly used in modern JavaScript.

Dynamic Messages

let username = "Tanishka";

let message = `Welcome back, ${username}!`;

Product Information

let product = "Headphones";
let price = 1999;

console.log(`The ${product} is available for ₹${price}.`);

Student Information

let name = "Tanishka";
let subject = "JavaScript";
let marks = 85;

console.log(`Student ${name} scored ${marks} marks in ${subject}.`);

Dynamic URLs

let userId = 25;

let url = `https://example.com/users/${userId}`;

Template literals are especially useful when information changes depending on variables.


11. Template Literals with Objects

We can also use object properties inside template literals.

let student = {
    name: "Tanishka",
    course: "Computer Engineering"
};

console.log(`My name is ${student.name} and I study ${student.course}.`);

Output:

My name is Tanishka and I study Computer Engineering.

This is useful when working with data from APIs or databases.


12. Things to Remember

There are a few simple things to remember when using template literals.

Use Backticks

Correct:

`Hello World`

Not:

"Hello World"

or:

'Hello World'

Use ${} for Variables

Correct:

`Hello ${name}`

If we write:

`Hello name`

JavaScript will display name as normal text.

Don't Forget the Closing Backtick

A template literal needs both an opening and closing backtick:

let message = `Hello World!`;

13. Template Literals vs String Concatenation

Feature String Concatenation Template Literals
Syntax Uses + Uses backticks
Variables Uses + Uses ${}
Readability Can become difficult Easier
Multi-line strings Less convenient Very easy
Expressions Possible Easy with ${}
Dynamic content Possible Very convenient

Both methods are valid, but template literals are usually more convenient when working with dynamic strings.

14. Complete Example

Let's combine the concepts we have learned:

let name = "Tanishka";
let age = 20;
let course = "JavaScript";
let marks = 85;

let result = `
Student Details
---------------
Name: ${name}
Age: ${age}
Course: ${course}
Marks: ${marks}
Status: ${marks >= 40 ? "Pass" : "Fail"}
`;

console.log(result);

Output:

Student Details
---------------
Name: Tanishka
Age: 20
Course: JavaScript
Marks: 85
Status: Pass

This example uses:

  • Variables

  • String interpolation

  • Multi-line strings

  • Expressions

  • A conditional operator


Conclusion

Template literals are a simple but very useful feature of modern JavaScript.

Before template literals, we commonly used the + operator to combine strings and variables. This worked, but longer strings could become difficult to read.

Template literals make this process much easier.

They allow us to use backticks to create strings and ${} to insert variables or expressions.

For example, instead of:

"Hello " + name + ", welcome to JavaScript!"

we can write:

`Hello ${name}, welcome to JavaScript!`

Template literals are useful for dynamic messages, multi-line text, HTML content, product details, URLs, calculations, and much more.

Once we start using template literals, our JavaScript code becomes cleaner, shorter, and easier to understand.

Less +, less confusion, and cleaner JavaScript.