Curriculum Series

What are template literals?

What are template literals?

What are template literals?

JavaScript

The short answer

Template literals are strings wrapped in backticks (`) instead of quotes. They support string interpolation with ${}, multi-line strings without escape characters, and tagged templates for custom processing. They were introduced in ES6 and are the preferred way to build strings in modern JavaScript.

String interpolation

javascript
const name = 'John';
const age = 30;
// Old way — string concatenation
const msg1 =
'Hello, ' + name + '. You are ' + age + ' years old.';
// Template literal — much cleaner
const msg2 = `Hello, ${name}. You are ${age} years old.`;

Any JavaScript expression works inside ${}:

javascript
`Total: $${(price * quantity).toFixed(2)}`;
`Status: ${isActive ? 'Active' : 'Inactive'}`;
`Items: ${items.length}`;

Multi-line strings

javascript
// Old way — need \n for new lines
const html1 = '<div>\n <p>Hello</p>\n</div>';
// Template literal — just press enter
const html2 = `
<div>
<p>Hello</p>
</div>
`;

Expression evaluation

You can put any expression inside ${}, including function calls:

javascript
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
`Hello, ${capitalize(name)}!`; // "Hello, John!"

Interview Tip

Show interpolation and multi-line strings — these are the two main features. Mention that any expression works inside ${}, not just variables. This is a quick question that does not need a long answer.

Why interviewers ask this

This tests basic ES6 knowledge. Template literals are used everywhere in modern JavaScript, and interviewers want to confirm you know the syntax.

Finished reading?

Mark this topic as solved to track your progress.