What is Template Literal in JS?

Hi all. I’ve already know ow to use a string, for example:

console.log("Hello, world!"); // This prints Hello, world!.

But what if I use a string, like this?

var a = 5;
var b = 5;
var result = a + b;
console.log(`${a} + ${b} is equal to ${result}!`); // Output 5 + 5 is equal to 10!.

Does this one means Template Literals? And what does it means??

Please answer. :slight_smile:

Pummarin :slight_smile:

If you write something between the backticks, as you did in your second block of code, that’s a template literal. The backticks are an alternative to quotes (") or (’).

The main use I’ve seen is to allow the expressions to be embedded (in other words put those variables right in there), so you don’t have to write

"My dog is " + dogYears + " years old."

If you read the docs, you’ll get more information and cases.

1 Like