1

ES6 introduced "Tagged template literals", allowing you to find the output of interpolated code (probably not the best term)

Example (ripped from MDN)

var person = 'Mike';
var age = 28;

function myTag(strings, personExp, ageExp) {

  var str0 = strings[0]; // "that "
  var str1 = strings[1]; // " is a "

  // There is technically a string after
  // the final expression (in our example),
  // but it is empty (""), so disregard.
  // var str2 = strings[2];

  var ageStr;
  if (ageExp > 99){
    ageStr = 'centenarian';
  } else {
    ageStr = 'youngster';
  }

  return str0 + personExp + str1 + ageStr;

}

var output = myTag`that ${ person } is a ${ age }`;

console.log(output);

I see how you would use it, my question is why? It seems completely useless, and I'd just like to know what it's for.

Geordie Fischer
  • 117
  • 1
  • 1
  • 7
  • If if nothing more, they are syntactical sugar. "myTag\`that ${ person } is a ${ age }\`;" is cleaner than "myTag(['that ', ' is a '], person, age)", don't you think? – Tibrogargan Apr 23 '17 at 07:27

0 Answers0