Template literals (`...${}...`)
Builds strings with interpolation and multi-line support using backticks.
const msg = `Hello ${name}, you have ${count} items.`;This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Builds strings with interpolation and multi-line support using backticks.
Template literals are string literals delimited by backtick (`) characters, allowing for multi-line strings, string interpolation with embedded expressions, and tagged templates. They were introduced in ES6 to provide a more readable and concise syntax compared to traditional string concatenation using the '+' operator. Expressions within the ${expression} placeholders are evaluated at runtime and coerced to strings. While performance is generally comparable to standard concatenation in modern engines like V8, template literals offer significant maintenance benefits for complex UI strings. Note that template literals do not support legacy octal escape sequences, and both 'null' and 'undefined' values are coerced to their literal string representations ('null' and 'undefined'). IE is not supported.
Quick reference
Syntax
const msg = `Hello ${name}, you have ${count} items.`;
See it in practice
Examples
Basic String Interpolation
const username = 'Alice';
const score = 95;
const message = `User ${username} scored ${score} points.`;
console.log(message);User Alice scored 95 points.
Variables are embedded directly into the string without needing to break the string with plus operators.
Multi-line Strings and Formatting
const items = ['Apples', 'Oranges'];
const html = `
<ul>
<li>${items[0]}</li>
<li>${items[1]}</li>
</ul>`;
console.log(html);<ul>\n <li>Apples</li>\n <li>Oranges</li>\n</ul>
Template literals preserve all whitespace and newlines, making them ideal for generating HTML fragments.
Embedded Expressions and Logic
const price = 100;
const tax = 0.08;
const total = `Total: $${(price * (1 + tax)).toFixed(2)}`;
console.log(total);Total: $108.00
Any valid JavaScript expression can be placed inside ${}, including arithmetic operations and function calls.
Debug faster
Common Errors
LogicError
Cause: Attempting to use interpolation syntax inside standard single (') or double (") quotes.
Fix: Always use backticks (`) when you need to use the ${} placeholder syntax.
const name = 'JS'; const msg = 'Hello ${name}';ReferenceError
Cause: Referencing a variable inside a template literal before it has been declared or initialized.
Fix: Ensure all variables used in interpolation are defined in the current scope before the literal is evaluated.
const greeting = `Hi ${user}`; let user = 'Dev';LogicError
Cause: Unexpected coercion of null or undefined values into the literal strings 'null' or 'undefined'.
Fix: Use a logical OR or ternary operator to provide a default value inside the placeholder.
const data = null; const msg = `Value: ${data || 'N/A'}`;Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Builds strings with interpolation and multi-line support using backticks.
LogicError: Always use backticks (`) when you need to use the ${} placeholder syntax. ReferenceError: Ensure all variables used in interpolation are defined in the current scope before the literal is evaluated. LogicError: Use a logical OR or ternary operator to provide a default value inside the placeholder.