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>