Template Literals
Introduction
Template literals
are a new and powerful way to handle strings in JavaScript.
They provide a cleaner and easier to read syntax for string interpolation,
multi-line strings, and more.
Template Literals
Previous String Concatenation Method
Before ES6 and Template Literals
in order to combine strings
we would do this:
Let's explore now, what Template literals do.
Syntax
Template literals use backticks (
)instead of quotes and allow embedded expressions using
$`.
Basic example
Multi-line Strings
Template literals
make it easy to create multi-line strings
without using escape characters.
Embedded Expressions
Template literals
support embedding any valid Javascript expression.
Tagged Template Literals
Tagged template literals allow you to parse template literals with a function
.
Template Literals in HTML
Template literals can be used to create HTML templates
dynamically.
CSS in Template Literals
Template literals can also be used with CSS-in-JS libraries
to define styles dynamically.