agyanadda@gmail.com

JS Template Literals

Strings and Numbers 1 min read Updated Dec 1, 2026 Topic 10 of 38

Template literals are strings written with backticks `.

Insert values with ${ }

const name = 'Asha';
const msg = `Hello ${name}, you have ${2 + 3} messages`;

Anything inside ${ } is evaluated: variables, maths, function calls.

Multi-line strings

Template literals keep line breaks, which makes them perfect for building HTML:

const card = `
  <div class="card">
    <h3>${title}</h3>
  </div>`;

They are easier to read than joining strings with +.

Example

Example — Building HTML with template literals HTML
<div id="list"></div>

<script>
  const courses = [
    { name: 'HTML', topics: 33 },
    { name: 'CSS', topics: 39 },
    { name: 'JavaScript', topics: 40 }
  ];
  let html = '';
  for (const c of courses) {
    html += `<p><strong>${c.name}</strong> – ${c.topics} topics</p>`;
  }
  document.getElementById('list').innerHTML = html;
</script>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which character starts a template literal?

Exercise 2 Fill in the Blank

Inside a template literal, insert a variable with ___{name}.

Exercise 3 True / False

Template literals can span multiple lines.

Frequently Asked Questions

No. User input may contain HTML or scripts. Use textContent for user input, or escape it first.