JavaScript Template Literals and Tagged Templates
Quick answer
Template literals use backticks to construct strings with embedded expressions and multiline content. A tagged template instead calls a function with literal segments and expression values.
How it works
In an untagged template, ${expression} evaluates and converts its result to text. A tag receives the template object first and substitutions as later arguments; it can return any value. String.raw reads raw segments rather than processed escapes. A template does not automatically escape data for HTML or SQL.
const qty = 3;console.log(`Total: ${qty * 4}`);Expected output in Node.js:
Total: 12The embedded expression is evaluated before interpolation.
function inspect(parts, value) { return [parts[0], value, parts[1]].join("|"); }console.log(inspect`x=${7}!`);Expected output in Node.js:
x=|7|!The tag receives two literal parts and the numeric substitution.
Common mistakes
Do not put ${...} inside normal quoted strings and expect interpolation. Use the proper delimiters.
Practice
Produce a receipt label with an interpolated subtotal and explain why rendering it as text is different from HTML insertion.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Template Literals and Tagged Templates — Knowledge Check
Apply the taught concept and predict the output.
Which delimiters enable ordinary template interpolation?
What is the first argument to a template tag?
Does an untagged template automatically prevent unsafe HTML insertion?
What is the exact console output in JavaScript Template Literals and Tagged Templates example 1?
const qty = 3;console.log(`Total: ${qty * 4}`);What is the exact console output in JavaScript Template Literals and Tagged Templates example 2?
function inspect(parts, value) { return [parts[0], value, parts[1]].join("|"); }console.log(inspect`x=${7}!`);