Template Literals

Example

Backtick strings can embed expressions with ${...} and span multiple lines:

const name = "Asha", items = 3, price = 249.5;
console.log(`Hi ${name}, you have ${items} items. Total: ₹${(items * price).toFixed(2)}`);
const html = `
<ul>
  ${["HTML", "CSS"].map(t => `<li>${t}</li>`).join("")}
</ul>`;
console.log(html.trim());

Output

Hi Asha, you have 3 items. Total: ₹748.50
<ul>
  <li>HTML</li><li>CSS</li>
</ul>

Caution with HTML

Note: Inserting user input into HTML with template literals and innerHTML allows XSS attacks. Use textContent for user-provided text, or escape it first.