Created
September 15, 2019 17:22
-
-
Save paigen11/551da8fe131854ea2c9fca7a1cb1432f to your computer and use it in GitHub Desktop.
Example using tagged templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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