December 2018
Intermediate to advanced
642 pages
15h 5m
English
A tagged template is a more advanced form of the templates we've been looking at. Basically, it's another way to call a function, but with a syntax similar to a template string. Let's look at an example and then explain it:
// Source file: src/tagged_templates.jsfunction showAge(strings, name, year) { const currYear = new Date().getFullYear(); const yearsAgo = currYear - year; return ( strings[0] + name + strings[1] + year + `, ${yearsAgo} years ago` );}const who = "Prince Valiant";const when = 1937;const output1 = showAge`The ${who} character was created in ${when}.`;console.log(output1);// The Prince Valiant character was created in 1937, 81 years agoconst model = "Suzuki";const yearBought = 2009;const output2 = showAge`My ...Read now
Unlock full access