Tagged template literals
Put a function in front of a backtick string and rewrite the interpolation.
A tag is just a function called with the template's literal string pieces and its interpolated values handed over separately β letting you escape, transform, or capture every ${value} before it ever joins the surrounding text, which is the foundation of html-escapers, styled-components, and parameterised SQL builders.
A tag is a function that receives the pieces
A tagged template is a function name placed directly before a backtick literal: tag`Hello ${name}`. Instead of building one finished string, the runtime calls tag with the static text pieces as its first argument (an array strings) and each interpolated value as the remaining arguments. So greet`Hi ${name}, you are ${age}` calls greet(["Hi ", ", you are ", ""], name, age). The strings array always has exactly one more entry than the number of ${β¦} slots β there is a leading and trailing piece even when they are empty strings. Collecting the values with a rest parameter (...values) is the idiomatic signature.
You control how pieces and values rejoin
Because the tag sees text and values separately, it decides how they recombine β or whether they recombine at all. The plain-string behaviour is just strings.reduce((acc, s, i) => acc + (i ? String(values[i - 1]) : "") + s)-style stitching, but you can transform each value first: uppercase it, escape it, wrap it in markers, or push it into an array and emit a placeholder instead. This separation is the whole point: the values are never trusted as raw text, so a tag can neutralise them before they touch the literal β exactly what an html-escaper or a SQL parameter builder needs.
String.raw and the .raw property
Every strings array carries a companion strings.raw array holding the un-escaped source text β backslash sequences like \n appear as the two characters backslash-n rather than a newline. The built-in String.raw tag uses this to return the literal source verbatim, which is why String.raw`C:\new\table` keeps its backslashes intact. Reach for .raw whenever the backslashes matter (Windows paths, regex sources, LaTeX); reach for the cooked strings array when you want the normal escape processing.
Building a tiny DSL
Once a tag returns whatever it likes β a string, an object, an array of parts β tagged templates become a lightweight DSL syntax. An html tag can escape interpolations and hand back safe markup; a sql tag can replace each value with a numbered placeholder ($1, $2, β¦) and collect the values into a separate params array, giving you parameterised queries that read like plain SQL. The examples below build both.
Try it 5 examples
A tag receives strings and values separately
JSintroThe tag receives strings as ["", " is a ", "!"] and the values as ["Chiron", "tutor"] β separate arrays, so the 3 static pieces always outnumber the 2 ${} slots by one. This split is what lets a tag inspect or neutralise each value before it ever touches the surrounding text.