Sometimes you need to combine multiple strings with constants or variables, for example when printing "My name is".
If you use normal string concatenation, it looks like this:
const name = "Kayn";console.log("My name is " + name);That style has a few disadvantages.
"Hello " + firstName + " " + lastName + ", you are " + age + " years old.";\n:"Line1\nLine2\nLine3";That is why JavaScript has template literals, a newer form of string literal:
`.${}.\n.The syntax looks like this:
const name = "Kayn";console.log(`My name is ${name}`);Advantages:
${variable} is closer to natural language and easier to scan.`Hello ${firstName} ${lastName}, you are ${age} years old.`;\n.`Line1Line2Line3`;`Next year, you'll be ${age + 1} years old.`;