Category
JavaScript
Tags
Cornerstone concept
Template literals are a way of creating strings in JavaScript that allow for the embedding of expressions within the string using the ${expression}
syntax.
Template literals are created using backticks (`) instead of single or double quotes.
For example, instead of concatenating strings using the +
operator like this:
const name = "John";
const message = "Hello, " + name + "!";
You can use a template literal like this:
const name = "John";
const message = `Hello, ${name}!`;
This allows for more concise and readable code, especially when working with longer strings or when multiple expressions need to be embedded within the string.
Template literals also support multi-line strings, which can be helpful for formatting text or HTML code.