68
Tagged Template Literals (Simple Example)
2021-04-28 - JavaScript
1// JS Tip of the Day
2// Tagged Template Literals (Simple Example)
3
4const hero = 'Batman';
5const anotherHero = 'Superman';
6
7function uppercaseTagged(strings, ...values) {
8 console.log(strings); // ['Film: ', 'v', ': Dawn of Justice']
9 console.log(values); // ['Batman', 'Superman']
10 let strWithUppercase = '';
11 strings.forEach((str, i) => {
12 strWithUppercase += str + (values[i] || '').toUpperCase();
13 });
14 return strWithUppercase;
15}
16
17console.log(uppercaseTagged`Film: ${hero} v ${anotherHero}: Dawn of Justice`);
#JStipoftheday