Template Literals
Explore the ES6 template literals feature to simplify string interpolation and multi-line strings. Learn to use backticks, embed expressions with ${}, nest templates, apply ternary operators, and pass functions within template literals for cleaner, more readable JavaScript code.
Template literals were called template strings prior to ES6… Let’s have a look at what’s changed in the way we interpolate strings in ES6.
Interpolating strings #
We used to write the following in ES5 in order to interpolate strings:
In ES6, we can use backticks to make our lives easier. We also need to wrap our variable names in ${}
In ES6 we can use backticks to reduce our typing:
...