Template literals use backticks (`) and add two things plain string quotes lack: interpolation of expressions and multi-line strings.
js
name = ;
count = ;
msg = ;
total = ;
html = ;
Template literals use backticks (`) and add two things plain string quotes lack: interpolation of expressions and multi-line strings.
name = ;
count = ;
msg = ;
total = ;
html = ;
Before template literals you'd write "Hi " + name + ", " + count + ... — verbose and error-prone. Interpolation reads far better.
A function placed before the backticks receives the string parts and the interpolated values, letting it process them:
function highlight(strings, ...values) {
return strings.reduce((out, s, i) =>
out + s + (values[i] ? `<b>${values[i]}</b>` : ""), "");
}
highlight`Hello ${name}!`; // "Hello <b>Ann</b>!"
This powers libraries like styled-components and safe SQL/HTML escaping helpers.
Template literals are the standard way to build strings today — readable interpolation, easy multi-line text, and a hook (tagged templates) for DSLs.