Skip to main content

Command Palette

Search for a command to run...

Template Literals in JavaScript

Updated
•4 min read•View as Markdown
K
Hi, I'm Kanishka Shashi. I'm a Computer Science (AI & ML) student passionate about building, analyzing, and explaining technology. This blog is where I simplify complex concepts into practical, beginner-friendly guides. You'll find articles on Web Development, System Design, AI & LLMs, Data Analytics, SQL, Git/GitHub, and software engineering best practices. I enjoy exploring how modern technologies work under the hood and sharing insights through real-world examples and hands-on projects. Whether you're a student, developer, or tech enthusiast, I hope these articles help you learn something new and build better software. Learning in public. Building consistently. Sharing everything I discover.

Working with strings is very common in JavaScript. But earlier, combining strings and variables was not very clean or readable.

That’s where Template Literals come in—they make string handling simpler, cleaner, and more powerful.

In this blog, we’ll learn:

  • Problems with traditional string concatenation

  • Template literal syntax

  • How to embed variables in strings

  • Multi-line strings

  • Real-world use cases


1. Problem with Traditional String Concatenation

Before template literals, we used + (plus operator) to join strings.

Example:

let name = "Rahul";
let age = 21;

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

Output:

My name is Rahul and I am 21 years old.

Issues with This Approach

  • Hard to read when strings get long

  • Too many + signs

  • Easy to make mistakes

  • Looks messy with multiple variables


2. Template Literal Syntax

Template literals use backticks ( ) instead of quotes.

Example:

let name = "Rahul";
let age = 21;

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

console.log(message);

Key Points:

  • Use backticks → `

  • Use ${} to insert variables

This is called string interpolation.


3. Embedding Variables in Strings

With template literals, we can directly insert variables.

Example:

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

console.log(`The price of \({product} is ₹\){price}`);

You Can Also Use Expressions

let a = 10;
let b = 5;

console.log(`Sum is ${a + b}`);

Output:

Sum is 15

👉 You can even perform calculations inside ${}


4. Multi-line Strings

Before template literals, writing multi-line strings was difficult.

Old Way:

let text = "Hello\n" +
           "Welcome to JavaScript\n" +
           "Learning is fun!";

With Template Literals

let text = `Hello
Welcome to JavaScript
Learning is fun!`;

console.log(text);

Output:

Hello
Welcome to JavaScript
Learning is fun!

👉 Much cleaner and easier!


5. Before vs After (Comparison)

Old Method:

let name = "Aman";
let msg = "Hello " + name + ", welcome!";

Template Literal:

let msg = `Hello ${name}, welcome!`;

Visualization

Before:
"Hello " + name + ", welcome!"

After:
`Hello ${name}, welcome!`

👉 Clearly more readable!


6. String Interpolation Diagram

Template Literal:
`Hello ${name}`

Step:
name = "Riya"

Final Output:
"Hello Riya"

7. Real-World Use Cases

Template literals are widely used in modern JavaScript.


1. Dynamic Messages

let user = "Kanishka";
console.log(`Welcome back, ${user}!`);

2. HTML Generation

let title = "JavaScript Basics";

let html = `<h1>${title}</h1>`;

3. Logging Information

let score = 95;
console.log(`Your score is ${score}`);

4. Combining Multiple Values

let firstName = "John";
let lastName = "Doe";

console.log(`Full Name: \({firstName} \){lastName}`);

8. Why Template Literals Are Better

1. Improved Readability

Code looks clean and easy to understand.


2. Less Error-Prone

No need to worry about missing + signs.


3. Supports Multi-line Strings

No extra characters needed.


4. Easy Variable Insertion

Use ${} instead of complex concatenation.


9. Practice Assignment

Task 1:

let name = "YourName";
let age = 20;

👉 Print:

My name is YourName and I am 20 years old.

Task 2:

let a = 5;
let b = 10;

👉 Print sum using template literals


Task 3:

Create a multi-line message:

Hello
Welcome to JavaScript
Keep Learning!

10. Final Thoughts

Template literals are one of the most useful features in JavaScript.

Key takeaways:

  • Use backticks `

  • Insert variables using ${}

  • Supports multi-line strings

  • Makes code clean and readable

Once you start using template literals, you’ll rarely go back to old string concatenation methods.