JavaScript Template Literals: Strings with Superpowers

Imagine youâre typing a WhatsApp message. You donât just want to send plain textâyou might want to drop in an emoji đ, the current time, or even your friendâs name automatically. Thatâs exactly what Template Literals do for strings in JavaScriptâthey make them smarter, prettier, and a lot easier to work with.
So⌠what are Template Literals?
Normally, you wrap strings in single quotes ('Hello') or double quotes ("Hello").
Template Literals use backticks instead:
`Hello World`
Those little slanted ticks (`) arenât just for decorationâthey unlock a whole set of new powers.
Power #1 â Multiline Strings (Goodbye \n)
With regular strings, if you want text on multiple lines, you have to use the \n character:
"Hello\nWorld"
But Template Literals let you press Enter right inside the quotes:
const message = `Game Over!
John's score was 740.`;
console.log(message);
No weird symbolsâjust clean, readable text, exactly how it will appear.
Power #2 â String Interpolation (Magic Insertion)
Say you have a playerâs name stored in a variable:
const name = "John";
const score = 74;
With old-school strings, youâd write:
"Hello " + name + "! Your score: " + score
With Template Literals, you can embed variables or even calculations inside ${ }:
console.log(`Game Over!
${name}'s score was ${score * 10}.`);
JavaScript replaces ${name} with the value of name, and ${score * 10} with the calculated resultâright inside the string.
Power #3 â Tagged Templates (The Expertâs Tool)
Hereâs where it gets fancy. You can put a function name right before the backtick. This âtag functionâ gets the stringâs static parts and the inserted values separately.
For example:
function customTag(strings, ...values) {
console.log(strings); // Static parts
console.log(values); // Inserted values
}
customTag`Hello ${name}, score: ${score}`;
Why is this useful? Because you can filter, format, or even transform the output into something completely differentâlike secure HTML, formatted currency, or database queries.
Power #4 â Raw Strings
Normally, \n means ânew line.â But what if you want JavaScript to treat it literally as \n?
console.log(String.raw`Line1\nLine2`);
// Output: Line1\nLine2
String.raw keeps every backslash and special character exactly as typedâperfect for regex patterns or file paths.
Power #5 â Building HTML with Ease
Because Template Literals can handle multiline text and variable embedding, theyâre perfect for generating HTML:
const username = "Alex";
const html = `
<div>
<h1>Welcome, ${username}!</h1>
</div>
`;
Combine them with a tag function that escapes unsafe characters, and youâve got safe, dynamic HTML generation without messy concatenation.
Fill-in-the-Blank Practice
- Template Literals are written using ________ instead of single or double quotes.
- You can insert variables into a Template Literal using the syntax ${________}.
- The ability to insert variables or expressions directly into strings is called ________.
- In Template Literals, pressing ________ inside the string automatically creates a new line without
\n. - A ________ function can be placed before a Template Literal to process its content before it becomes a final string.
- The
String.________method returns the raw text of a Template Literal, ignoring escape sequences like\n. ${score * 10}inside a Template Literal will first ________ the expression before placing it in the string.- Template Literals make it easier to create ________ text blocks without messy string concatenation.
- In a tagged template, the first argument to the tag function is an array of the ________ parts of the string.
- Template Literals are especially useful for building ________ content dynamically in JavaScript.