Tagged template literals

Use ‘tagged template literals’ to run a template string through a function.

Explanation

  • Tagged template literals allow you to parse template literals with a function.
  • The function receives the template strings and the values as arguments.

Usage

To use tagged template literals, define a function and use it with a template literal:

function boldMe(strs, ...vals) {
  let str = strs.map(
    (string, i) => `${string}${vals[i] ? `<b>${vals[i]}</b>` : ``}`
  );
  return str.join('');
}

const make = 'Porsche';
const model = '718 Cayman';
const sentance = boldMe`My next car is a ${make} ${model}`;
console.log(sentance);