Today we will talk about Template String and how can we use it in our javascript program.
Introduction
Template String introduces in Ecma Script 6 (ES6) in 2015. It is the different type of expression where we can concatenate our string in the single variable. The template string is enclosed by the back-tick character () instead of single or double-quotes.
let name = "Akhil"; let age = 24; let address = "India"; console.log("Hello My name is " + name + " and I am " + age + " years old and I am from " + address); Output:- Hello My name is Akhil and I am 24 years old and I am from India
On the above example, we need to display the Name, age, address in a single line and for doing that we have concatenated the variable multiple time by using double quotes open and closed with arithmetic (+) operator
Let’s check the same example using string template
let name = "Akhil"; let age = 24; let address = "India"; console.log(`Hello My name is ${name} and I am ${age} Years old and I am from ${address}`); Output:- Hello My name is Akhil and I am 24 years old and I am from India
In this example, you can see how concatinating is going to easy there is no need to use an arithmetic operator or double quotes open and close. It will very helpful when you write the dynamic html code in javascript.
I hope you enjoyed this article, we will have some more useful articles in the future which will be helpful for the developer’ daily coding life.
I will see you again with the new topic and have a great day!