JavaScript, inside out · Chapter 37 of 51
Tagged templates
By the end of this chapter you can put a function in front of a template literal and decide yourself how every ${} value goes in: formatted, escaped, kept apart or left raw. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Modern JavaScript
- 6 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
A customer signed up with the name <b>Raju</b>, and every web receipt for him came out in bold. Kabir points out that the next customer could type a <script> instead. Meera wants every amount on the screen written the Indian way, ₹1,25,000, not 125000. The kitchen printer needs its codes, like \n, sent exactly as typed, and Kabir never wants a customer's text pasted into a database query.
A plain template literal glues each value in before you can touch it. So far, you would call a helper inside every placeholder, ${escape(name)}, in every template in the app. Miss one, and that is the bug.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
inspect: what a tag function receives
Write the tag inspect. Return an object with three keys: strings (the text pieces), raw (the raw text pieces, exactly as typed) and values (the ${} values, in order). strings and raw must be plain arrays you made yourself: the pieces array JavaScript passes in is frozen, and the copy must not be.
inspect
Chai for ${'Ravi'} at ${40}gives { strings: ['Chai for ', ' at ', ''], raw: ['Chai for ', ' at ', ''], values: ['Ravi', 40] }. A template that is only a value gives two empty pieces, and with an escape such as \n the strings piece holds a real newline while the raw piece keeps the backslash and the n. - Task 2
rupees: every number in a message as rupees
Write the tag rupees. Return the message as one string, with every value that is a number written as ₹ followed by the number grouped the Indian way (125000 becomes ₹1,25,000, 0 becomes ₹0), and every other value, including text that looks like a number such as '1042', put in as it is. The template's own text is unchanged.
rupees
Masala chai ${40}, total ${125000}gives 'Masala chai ₹40, total ₹1,25,000'. A template with no values gives its text unchanged, and one that is only a value, such as a lone 40, gives '₹40'. - Task 3
safeHtml: receipt HTML a customer's name cannot break
Write the tag safeHtml. Return one string in which every value is turned into text and then escaped: & becomes &, < becomes <, > becomes >, " becomes " and ' becomes '. The template's own text is never escaped.
safeHtml
<p>${'<b>Raju</b>'}</p>gives '<p><b>Raju</b></p>', and a number value such as 40 comes out as 40. Escape the & first, so a value that already reads < becomes &lt; (escaped exactly once). A template with no values gives its text unchanged. - Task 4
printerCode: printer codes exactly as typed
Write the tag printerCode. Return one string where the template text is kept exactly as typed, so a \n or \t in the template stays a backslash and a letter (two characters, not a newline or a tab), and every value is turned into a string and upper-cased (the number 2 stays 2).
printerCode
\n${'chai'}gives a backslash, n, then CHAI (written '\\nCHAI' in code). A template with no values, such as one reading no values followed by \n, is 11 characters long. - Task 5
sql: customer input kept out of the query text
Write the tag sql. Return an object with two keys: text, the template text with each value replaced by a numbered placeholder ($1 for the first value, $2 for the second, and so on), and values, the values themselves in order, unchanged.
sql
SELECT * FROM orders WHERE item = ${'chai'}gives { text: 'SELECT * FROM orders WHERE item = $1', values: ['chai'] }. Values placed side by side give '$1$2$3', and a template with no values gives its text and an empty values array. - Task 6 · interview question
Interview: prediction, the comma operator, void and globalThis
Export prediction, an object with exactly the five keys comma, voided, globalType, sameGlobal and separator. Each one holds the value its expression in the snippet above it gives, as a real value (a number, undefined, a string or true or false), not text describing it.
The check works out the same five expressions and compares.
What you will learn
- Tag functions: a function written before the backticks gets the text pieces and the values separately.
- Rebuilding the text: stitch the pieces back together, changing each value on the way.
- Raw strings: strings.raw and String.raw keep backslashes as typed.
- Three small operators: the comma operator, void and globalThis, all favourites in interviews.
More in Modern JavaScript
Build "Tagged templates" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.