What are template literals?
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
1const name = 'John';2const age = 30;34// Old way — string concatenation5const msg1 =6 'Hello, ' + name + '. You are ' + age + ' years old.';78// Template literal — much cleaner9const msg2 = `Hello, ${name}. You are ${age} years old.`;
Any JavaScript expression works inside ${}:
1`Total: $${(price * quantity).toFixed(2)}`;2`Status: ${isActive ? 'Active' : 'Inactive'}`;3`Items: ${items.length}`;
Multi-line strings
1// Old way — need \n for new lines2const html1 = '<div>\n <p>Hello</p>\n</div>';34// Template literal — just press enter5const html2 = `6<div>7 <p>Hello</p>8</div>9`;
Expression evaluation
You can put any expression inside ${}, including function calls:
1function capitalize(str) {2 return str.charAt(0).toUpperCase() + str.slice(1);3}45`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.