Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of template literals (backticks) following a function in ES6?

In GraphQL you can write something like this to define a query:

const USER_QUERY = gql`
  {
    user(id: 2) {
      name
    }
  }
`

In styled components you can define a styled component like this:

const Button = styled.button`
    background-color: papayawhip;
`

What is this syntax? I know with template literals you can sub in variables with this syntax: ${foo} but I have never seen this used. Any guidance would be appreciated.

like image 542
Kurt William Avatar asked Nov 27 '18 20:11

Kurt William


People also ask

What is the template literal in ES6?

Template literals are a new feature introduced in ECMAScript 2015/ ES6. It provides an easy way to create multiline strings and perform string interpolation. Template literals are the string literals and allow embedded expressions. Before ES6, template literals were called as template strings.

How do you use backticks in template literals?

In that case, the template literal is passed to your tag function, where you can then perform whatever operations you want on the different parts of the template literal. To escape a backtick in a template literal, put a backslash ( \ ) before the backtick. Dollar signs can be escaped as well to prevent interpolation.

What is the use of template literals in JavaScript?

Template literals provide an easy way to interpolate variables and expressions into strings. The method is called string interpolation.

What are backticks used for in JavaScript?

Backticks are an ES6 feature that allows you to create strings in JavaScript. Although backticks are mostly used for HTML or code embedding purposes, they also act similar to single and double quotes. Besides, using backticks makes it easier for string operations.


2 Answers

These are tagged template literals. The part before the backpacks is a reference to a function that will be called to process the string.

The function is passed the variables (the ${} parts) as arguments as well as the pieces of the string that surround the variables broken into an array. The return value of the function becomes the value of the template. Because of this very generalized format, you can do almost anything with the function. Here's a quick and dirty example that takes the variables (gathered into an array for convenience), makes them uppercase, and puts them back in the string:

function upperV(strings, ...vars) {
  /* make vars uppercase */
  console.log("vars: ", vars)       // an array of the passed in variables
  console.log("strings:", strings)  // the string parts

  // put them together
  return vars.reduce((str, v, i) => str + v.toUpperCase() + strings[i+1], strings[0]);
}

let adverb = "boldly"
let output = upperV`to ${adverb} split infinitives that no ${'man'} had split before...`;
console.log(output)
like image 191
Mark Avatar answered Oct 08 '22 10:10

Mark


Template literals have an additional feature called tagged templates. That's what the prefix before the opening backtick is. The prefix is actually the name of a function - the function is passed the constant parts of the template strings and the interpolated values (stuff in the ${} sections) and can process the resulting string into whatever it wants (although generally another string, doesn't have to be).

See this page on MDN for more details on how tagged templates work.

like image 14
Chris Tavares Avatar answered Oct 08 '22 11:10

Chris Tavares