⬅ Previous Topic
JavaScript Remove Elements – Remove DOM Elements EasilyJavaScript Template Literals – Modern Way to Handle Strings with ${Power}
Introduction to JavaScript Template Literals
In the traditional days of JavaScript, string handling involved messy concatenation, lots of +
signs, and cluttered readability. Enter ES6 template literals – a modern, elegant way to embed expressions, variables, and even functions directly inside strings using backticks ` `
.
Let’s walk through the powerful features of template literals step-by-step with clean examples and real-world inspiration.
Basic Syntax of Template Literals
Template literals use backticks instead of single or double quotes. They allow embedded expressions using the ${...}
syntax.
const name = "Alice";
const greeting = `Hello, ${name}!`;
console.log(greeting);
Hello, Alice!
Why Template Literals Are Better
- Improved readability
- Easy multi-line support
- Embed any valid JavaScript expression
- Great for dynamic HTML generation
Multiline Strings Without Escaping
No more \n
or ugly hacks. Just write the way you think it should appear.
const poem = `
Roses are red,
Violets are blue,
ES6 is awesome,
And so are you.
`;
console.log(poem);
Roses are red,
Violets are blue,
ES6 is awesome,
And so are you.
Embedding Expressions
Any valid JavaScript expression can be evaluated inside ${}
. This includes arithmetic, ternary operators, or even function calls.
const a = 5;
const b = 10;
console.log(`The sum of a and b is ${a + b}.`);
The sum of a and b is 15.
Using Functions Inside Template Literals
You can execute functions inline, making it highly dynamic.
function toUpper(str) {
return str.toUpperCase();
}
const language = "JavaScript";
console.log(`Welcome to ${toUpper(language)} tutorial!`);
Welcome to JAVASCRIPT tutorial!
Nested Template Literals
You can combine logic and structure like a pro. Nesting templates can simplify HTML generation.
const user = {
name: "Eve",
role: "Admin"
};
const message = `
<div>
<h2>Hello, ${user.name}!</h2>
<p>Role: ${user.role}</p>
</div>
`;
console.log(message);
Hello, Eve!
Role: Admin
Tagged Template Literals – Advanced Control
A tagged template allows you to parse the template literal with a function. Useful for escaping HTML, localization, or custom formatting.
function highlight(strings, ...values) {
return strings.reduce((acc, str, i) => acc + str + (values[i] ? "<strong>" + values[i] + "</strong>" : ""), "");
}
const lang = "JavaScript";
const output = highlight`Learning ${lang} is fun!`;
console.log(output);
Learning JavaScript is fun!
Dynamic HTML Generation Example
Template literals are perfect for generating HTML in JavaScript applications:
const items = ["Apple", "Banana", "Cherry"];
const html = `
<ul>
${items.map(item => `<li>${item}</li>`).join("")}
</ul>
`;
console.log(html);
- Apple
- Banana
- Cherry
Use Case: Email Template with Placeholder Data
Build email content dynamically:
const user = { name: "David", product: "Smartwatch" };
const email = `
Hi ${user.name},
Thank you for purchasing the ${user.product}. We hope you love it!
Cheers,
The Team
`;
console.log(email);
Hi David,
Thank you for purchasing the Smartwatch. We hope you love it!
Cheers,
The Team
Summary
Template literals are one of the most impactful features of modern JavaScript. They allow you to:
- Write cleaner, readable strings
- Embed values and expressions directly into strings
- Create dynamic, maintainable content for UI, emails, logs, and more
- Leverage tagging for advanced processing and security (like escaping HTML)
Once you get used to backticks and ${}
, you'll never want to go back to old-school string concatenation again. Let your strings breathe — the modern way.
⬅ Previous Topic
JavaScript Remove Elements – Remove DOM Elements Easily