โ† Back to list

Template Literals in JavaScript (With Real Examples) ๐Ÿš€

Vishal Bhangare ยท 2026-04-12 11:47 ยท 0 claps ยท 2.6 min read
#coding #web-development #es6 #javascript #software-development
Open on Medium โ†—
Wiki topics: ๐Ÿ’ป ยท Programming ๐ŸŒ ยท Web Development

Template Literals in JavaScript ๐Ÿš€

Introduction

In modern JavaScript (ES6+), Template Literals make working with strings much more powerful and readable. They allow you to embed variables, expressions, and even functions directly inside strings, eliminating messy concatenation.

Problems with Traditional String Concatenation

Before ES6, we used the + operator:

let name = "Virat";
let age = 18;
let city = "Bangalore";
let message = "My name is " + name + ", I am " + age + " years old, and I live in " + city + ".";
console.log(message);

Problems:

  • Difficult to read when variables increase
  • Easy to miss spaces or quotes
  • Hard to debug
  • Not scalable for dynamic content

Complex Example:

let product = "Laptop";
let price = 50000;
let text = "The product " + product + " costs Rs." + price + " and after discount it is Rs." + (price - 5000);
console.log(text);

Template Literal Syntax

Template literals use backticks (` `) instead of quotes.

let message = `Hello World`;

String Interpolation (Embedding Variables)

Using ${} we can directly insert variables.

let name = "Virat";
let age = 18;
let message = `My name is ${name} and I am ${age} years old.`;
console.log(message);

๐Ÿง  Behind the Scenes:

// Equivalent old version
"My name is " + name + " and I am " + age + " years old."

๐Ÿงฎ Embedding Expressions

You can perform calculations directly inside template literals.

let price = 50000;
let discount = 5000;
console.log(`Final Price: Rs.${price - discount}`);

More Advanced:

let a = 10;
let b = 5;
console.log(`Addition: ${a + b}`);
console.log(`Multiplication: ${a * b}`);
console.log(`Power: ${a ** b}`);

๐Ÿงฉ Embedding Functions

You can even call functions inside ${}.

function greet(name) {
  return `Hello, ${name}!`;
}
console.log(`Message: ${greet("Virat")}`);

๐Ÿ“„ Multi-line Strings

โŒ Old Way:

let text = "Line 1\n" +
           "Line 2\n" +
           "Line 3";

โœ… Template Literals:

let text = `Line 1
Line 2
Line 3`;
console.log(text);

โš”๏ธ Before vs After (Clear Comparison)

โŒ Old Style:

let user = "Virat";
let items = 3;
let msg = "Hello " + user + ", you have " + items + " new messages.";

โœ… Template Literal:

let user = "Vishal";
let items = 3;
let msg = `Hello ${user}, you have ${items} new messages.`;

๐Ÿ‘‰ Cleaner, shorter, and easier to understand

๐Ÿ—๏ธ Real-World Use Cases

1. Dynamic HTML (Very Important ๐Ÿ”ฅ)

let product = "Phone";
let price = 20000;
let html = `
  <div class="card">
    <h2>${product}</h2>
    <p>Price: Rs.${price}</p>
  </div>
`;
console.log(html);

2. Conditional Rendering

let isLoggedIn = false;
let message = `User is ${isLoggedIn ? "Logged In โœ…" : "Not Logged In โŒ"}`;
console.log(message);

3. Loop + Template Literals

let users = ["Vishal", "Rahul", "Ankit"];
let list = `
<ul>
  ${users.map(user => `<li>${user}</li>`).join("")}
</ul>
`;
console.log(list);

4. API Data Display

let user = {
  name: "Virat",
  age: 18,
  city: "Bangalore"
};
let info = `Name: ${user.name}, Age: ${user.age}, City: ${user.city}`;
console.log(info);
let info = `Name: ${user.name}, Age: ${user.age}, City: ${user.city}`;
console.log(info);

5. Logging & Debugging

let errorCode = 404;
let url = "/api/data";
console.error(`Error ${errorCode}: Cannot access ${url}`);

6. SQL Query (Backend Use)

let userId = 101;
let query = `SELECT * FROM users WHERE id = ${userId}`;
console.log(query);

๐Ÿ’ก Advanced Feature: Tagged Templates (Bonus ๐Ÿ”ฅ)

function highlight(strings, value) {
  return `${strings[0]}**${value}**${strings[1]}`;
}
let name = "Virat";
let result = highlight`Hello ${name}`;
console.log(result);

๐ŸŽฏ Why Template Literals Matter

  • โœ” Improves readability
  • โœ” Reduces bugs
  • โœ” Cleaner syntax
  • โœ” Supports dynamic content easily
  • โœ” Widely used in React, Node.js, APIs

๐Ÿ Conclusion

Template literals are a must-know feature for every JavaScript developer. They replace messy string concatenation with a clean, modern, and powerful approach.

Once you start using them, youโ€™ll never want to go back to " + " again ๐Ÿ˜„

โœจ Designed and crafted with โค๏ธ by Vishal


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
e2d37b4e78b1
slug
template-literals-in-javascript-with-real-examples-e2d37b4e78b1
url
https://medium.com/@bivshal27/template-literals-in-javascript-with-real-examples-e2d37b4e78b1
canonical_url
https://medium.com/@bivshal27/template-literals-in-javascript-with-real-examples-e2d37b4e78b1
author_url
https://medium.com/@bivshal27
status
ok
fetched_at
2026-06-11 05:11:55