Template Literals in JavaScript
๐ 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 = "Cherry"; let course = "BCom"; 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
