Template Literals in JavaScript (With Real Examples) ๐
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