Skip to content

Instantly share code, notes, and snippets.

@paigen11
Created September 15, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paigen11/551da8fe131854ea2c9fca7a1cb1432f to your computer and use it in GitHub Desktop.
Save paigen11/551da8fe131854ea2c9fca7a1cb1432f to your computer and use it in GitHub Desktop.
Example using tagged templates
var guy = 'Paul';
var age = 96;
function myTag(strings, personExp, ageExp) {
var str0 = strings[0]; // "That "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99){
ageStr = 'centenarian';
} else {
ageStr = 'youngster';
}
// You can even return a string built using a template literal - just to be fancy
return `${str0}${personExp}${str1}${ageStr}`;
}
var output = myTag`That ${ guy } is a ${ age }`;
console.log(output);
// prints: That Paul is a youngster
var lady = 'Elizabeth';
var age = 107;
var output = myTag`That ${ lady } is a ${ age }`;
console.log(output);
// prints: That Elizabeth is a centenarian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment