Full answer
A template literal is written between backticks instead of quotes. Inside it, ${...} evaluates any JavaScript expression and inserts the result as text, so Total: ${price * qty} replaces long chains of +. The literal can also stretch over several lines, and the line breaks stay in the string.
Values inside placeholders are converted to strings in the usual way. An object becomes [object Object] unless it has a custom toString, so print the field you need or use JSON.stringify.
A tagged template puts a function name before the backticks. The function receives an array of the fixed text pieces and then each inserted value as a separate argument. The text array always has one more item than the number of values. The function can return anything, not only a string. This is how libraries build safe SQL queries with parameters, escape HTML, and create styled components.
String.raw is a built-in tag that returns the text exactly as typed, without processing backslash escapes, which is useful for file paths and regular expression sources.
Template literals are not a security feature by themselves. Inserting user input into HTML or SQL still needs escaping.
const name = 'Dev';
const items = 3;
const price = 49.5;
console.log(`Hello ${name}, your bill is ${(items * price).toFixed(2)}.`);
console.log(`Line one
Line two`);
console.log(`Object in text: ${{ id: 1 }}`);
console.log(`JSON in text: ${JSON.stringify({ id: 1 })}`);
function escapeHtml(strings, ...values) {
const safe = (text) =>
String(text).replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
return strings.reduce(
(output, piece, i) => output + piece + (i < values.length ? safe(values[i]) : ''),
''
);
}
const comment = '<script>alert("x")</script>';
console.log(escapeHtml`<p>${comment}</p>`);
console.log(String.raw`C:\new\table`);
function describe(strings, ...values) {
return `${strings.length} pieces, ${values.length} values`;
}
console.log(describe`a${1}b${2}c`);Run with Node.js v24.22.1 as js-12.js; two runs gave identical output. Output: Hello Dev, your bill is 148.50. Line one Line two Object in text: [object Object] JSON in text: {"id":1} <p><script>alert("x")</script></p> C:\new\table 3 pieces, 2 values
Key points
- Backticks allow multi-line strings and ${expression} interpolation.
- Any expression is allowed in a placeholder; the result is converted to a string.
- A tag function receives the string pieces and the values separately.
- String.raw keeps backslashes as typed.
- Interpolation does not sanitize input.
Common mistakes
- Using a template literal for a string with no placeholders when plain quotes would do; harmless, but lint rules often flag it.
- Inserting an object directly and getting [object Object].
- Building HTML with user input in a plain template literal and creating an injection hole.
- Forgetting that a tag function receives the text pieces as an array and the values as separate arguments, not as one string.
Follow-ups
- How would you build a parameterized SQL query with a tagged template?
- What is the difference between strings and strings.raw inside a tag function?
- How do you include a backtick or ${ inside a template literal?
Tips
- Cover interpolation, multi-line text, and tags in that order; many candidates skip tags.
- Mention that tag functions can return non-strings; it signals real understanding.
- Warn about injection when talking about HTML or SQL.