What is a Template String in Advanced JavaScript?
A template string (also called a template literal) is a
feature introduced in ES6 (ECMAScript 2015) that
allows easier and more readable string creation,
especially when:
Embedding variables into strings
Writing multi-line strings
Using string expressions or functions inside strings
Syntax
Template strings are defined using backticks (`) instead of
single (') or double (") quotes.
Example
let user='Nishant Kumar';
let city='Pune';
//[Link](`Hello ${user} you stay in ${city}`);
//[Link](`Hello "${user}" you stay in "${city}"`);
//[Link](`Hello '${user}' you stay in '${city}'`);
let allin1=`Hello
${user}
you stay in
${city}`;
[Link](allin1);
Example 2 : How to use String literals in function
let firstName='Nishant';
let lastName='kumar';
function fullname(firstName,lastName){
return `${firstName} ${lastName}`;
}
let userDetail=`Hello ${fullname(firstName,lastName)}`;
[Link](userDetail);