📌 INTRODUCTION
Template literals are a modern feature introduced in ES6 (ECMAScript 2015) that make string handling in JavaScript much easier and cleaner.
Instead of using + for concatenation, template literals allow you to:
- Embed variables directly inside strings
- Write multi-line strings easily
- Improve code readability
📘 PROBLEM STATEMENT
👉 Write a program to convert string concatenation to template literals.
💡 CONCEPTS USED
- Template Literals (
` `) - String Interpolation (
${}) - ES6 Features
🧠 LOGIC EXPLANATION
- Create variables (e.g., name, course)
- Use template literals instead of
+operator - Embed variables using
${} - Display the result
💻 PROGRAM CODE (Using Template Literals)
let name = "Juhi";
let course = "MCA";
// Using template literals
let message = `Hello, my name is ${name} and I am studying ${course}.`;
console.log(message);
🖥️ OUTPUT
Hello, my name is Juhi and I am studying MCA.
🔍 STEP-BY-STEP EXPLANATION
- Backticks (
`) are used instead of quotes ${name}inserts variable value into string- No need for
+operator
⚡ OLD METHOD (String Concatenation)
let name = "Kavita";
let course = "MCA";
let message = "Hello, my name is " + name + " and I am studying " + course + ".";
console.log(message);
🎯 DIFFERENCE BETWEEN BOTH METHODS
| Feature | Concatenation | Template Literals |
|---|---|---|
| Syntax | Complex | Simple |
| Readability | Low | High |
| Multi-line | Difficult | Easy |
| Variable embedding | Using + | Using ${} |
🌟 ADVANTAGES OF TEMPLATE LITERALS
- Cleaner and modern syntax
- Easy variable insertion
- Supports multi-line strings
- Widely used in React & Node.js
📚 REAL-WORLD USE CASE
- Displaying dynamic messages
- Creating HTML templates in React
- Formatting API responses
❓ VIVA QUESTIONS (IMPORTANT)
Q1. What are template literals?
👉 Strings defined using backticks (`) that allow variable interpolation.
Q2. What is ${} used for?
👉 It is used to insert variables or expressions inside template literals.
Q3. Can template literals support multi-line strings?
👉 Yes, they support multi-line strings without \n.
Q4. What is the difference between " and `?
👉 " is used for normal strings, while ` is used for template literals.
Q5. Are template literals part of ES6?
👉 Yes, they were introduced in ES6.
🔗 RELATED POSTS
👉 MCA 168 Full Stack Development Lab Programs List
👉 Arrow Function in JavaScript – Program 1
📌 CONCLUSION
Template literals make JavaScript code more readable and efficient. They are essential for modern web development, especially when working with React and dynamic content.
👉 Practice converting all string concatenations into template literals — this is frequently asked in exams.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
