Skip to the solution
HardFrontend6 min read

What are tagged template literals?

588 views

The problem

Question presented to candidate: "What is a tagged template literal, and can you write one that automatically HTML-escapes every interpolated value, without escaping the literal string parts themselves?"

What a strong answer should cover:

  • πŸ“Œ Interview term: tagged template literal β€” a template literal immediately preceded by a function reference (the "tag"), which is called with the literal's pieces split apart: the static string segments as one array, and the interpolated values as separate, individual arguments β€” instead of the literal being auto-assembled into one plain string.
  • πŸ“Œ Interview term: strings and values β€” verified directly: the tag function's first parameter is a real array of the static text segments (one MORE element than the number of interpolations), and the remaining parameters (or a rest parameter) are the actual interpolated values, in order.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: a real auto-escaping tag function correctly HTML-escaped a genuinely dangerous interpolated string (<script>alert(1)</script>) while leaving the literal template text completely untouched β€” the exact real mechanism behind libraries like styled-components and `html```` template tags used for XSS-safe templating.
  • πŸ“Œ Interview term: strings.raw β€” verified directly: the strings array carries a .raw property holding the literal, unprocessed source text (a real backslash-n), distinct from the plain strings array itself, which holds the cooked, already-processed text (a real newline character) β€” a precise, verified distinction most candidates miss.
  • A precise answer names that a custom tag function can return anything, not just a string β€” a real, common pattern (used by libraries like styled-components and GraphQL's `gql````) returns a specialized object instead of a plain string, using the tagged template purely as a convenient syntax for structured input.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; writing a real, working auto-escaping tag function is the strong signal, beyond reciting the syntax.

Code / implementation expected: Yes β€” a real, working custom tag function (ideally the auto-escaping one from the prompt) is the clearest, most convincing demonstration.

  • tagged-templates

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, working auto-escaping tag function, plus the exact real strings/values shape and the raw-vs-cooked distinction

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 143 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What is currying?

Continue