concept template literal in category javascript

appears as: template literals, A template literal, template literal
Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

This is an excerpt from Manning's book Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond.

7.1. What are template literals?

A template literal is a new literal syntax for creating strings. It uses back-ticks (`) for demarcation instead of quotes (’ or ”) like string literals do, and supports new features and functionality. But template literals evaluate to a string just like string literals do:

let str1 = `Hello, World`;
let str2 = "Hello, World";
let str3 = `Hello, World`;          1
console.log(str1 === str3);         2
console.log(str2 === str3);         2
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":7},{\"line\":0,\"ch\":8}],[{\"line\":2,\"ch\":36},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":15},{\"line\":3,\"ch\":16}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":8}],[{\"line\":3,\"ch\":36},{\"line\":3,\"ch\":37}],[{\"line\":4,\"ch\":15},{\"line\":4,\"ch\":16}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":8}],[{\"line\":3,\"ch\":36},{\"line\":3,\"ch\":37}],[{\"line\":4,\"ch\":15},{\"line\":4,\"ch\":16}]]"}
!@%STYLE%@!
  • 1 Creating a string using a template literal
  • In this function, you processed the template literal the same as if it had not been tagged. But the point is to allow custom processing. This feature will most likely be implemented by library authors, but you can still use it to create some custom processing. Write a function that strips the whitespace between your markup:

    function stripWS() {
      let stringParts = arguments[0];
      let values = [].slice.call(arguments, 1);
      let str = stringParts.reduce(function(memo, nextPart) {
        return memo + String(values.shift()) + nextPart;
      });
      return str.replace(/\n\s*/g, '');
    }
    function getProductHTML(product) {
      return stripWS`
        <div class="product">
          <div class="product-image">
            <img alt="${product.name}" src="${product.image_url}">
          </div>
          <div class="product-desc">${product.desc}</div>
        </div>
      `;
    }

    We’ve only scratched the surface of what you can do with template literals. Because they allow for preprocessing of the interpolated values separately from the string parts of the literal and can return any data type, they’re perfect for creating abstractions known as DSLs (domain-specific languages), which you’ll be diving into next in your first capstone project.

    In this lesson, you learned the basics of template literals.

  • Template literals are a new literal syntax for creating strings using the back tick ` character.
  • Template literals can interpolate values with ${}.
  • Template literals can span multiple lines.
  • Template literals will open the door for domain-specific languages with tagged templates.