Backticks
Template literals are wrapped in ` characters, not normal quotes.
Template literals use backticks. They make interpolation, multiline text and readable generated strings much easier.
const topic = "JavaScript";
const count = 7;
const message = `${topic} has ${count} core examples.`;
console.log(message);
Strings, Numbers & Math
A template literal is a string created with backticks. Inside it, ${...} runs a JavaScript expression and inserts the result into the text.
Template literals can span multiple lines without escape characters. That makes them useful for readable messages, small snippets and formatted output.
Interpolation is powerful, but it is not automatic HTML escaping. If a value comes from a user or external source, do not inject it into innerHTML without sanitizing it.
Template literals are wrapped in ` characters, not normal quotes.
${expression} inserts the result of an expression into the string.
Line breaks can live directly inside the template literal.
Template literals do not make untrusted HTML safe.
Examples
const topic = "template literals";
const examples = 4;
const summary = `This page has ${examples} ${topic} examples.`;
console.log(summary);
const summary = "This page has " + examples + " " + topic + " examples."; // It works, but the final sentence is harder to read.
Code patterns
These small examples are designed for scanning. Use them when you need the syntax quickly, then read the surrounding notes when you want the deeper reason behind the pattern.
The common use case is readable value insertion.
const name = "JavaScript";
const message = `Current topic: ${name}`;
console.log(message);
Interpolation can calculate directly, but keep it readable.
const completed = 4;
const total = 7;
console.log(`Progress: ${completed}/${total}`);
Useful for logs or generated plain text.
const report = `Status report Ready: true Errors: 0`; console.log(report);
Do not hide too much logic inside ${...}.
const completed = 4;
const total = 7;
const percentage = Math.round((completed / total) * 100);
console.log(`Progress: ${percentage}%`);
Rules that matter
Strings and numbers are simple until they cross a boundary: form input, API data, generated output, rounding, formatting or search. Normalize, validate and format deliberately.
They make mixed text and values easier to read.
Move complex logic into named variables first.
Whitespace and line breaks become part of the string.
Template literals do not sanitize values.
The final text should be readable in the source.
Use \` if a literal backtick belongs inside the text.
Production thinking
Generated text is everywhere in interfaces. Template literals make that text easier to build without turning code into punctuation soup.
Readable generated messages are easier to keep clear for screen readers and live regions.
Production code should keep interpolation readable and keep unsafe HTML generation out of templates unless there is a safe rendering layer.
Template literals often build titles, descriptions or visible content. Keep output predictable and avoid accidental whitespace problems.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Explain the answer in plain language first. Then change the code example and verify that the result matches your explanation.