Goodbye Plus Signs: Why JavaScript Template Literals Feel Like Magic

I still have occasional, mild nightmares about the plus sign.
If you started writing JavaScript before 2015, you probably know exactly what I’m talking about. Building dynamic strings of text used to be an absolute, unmitigated chore.
Imagine you were building a user profile card. You needed to take some data—a user's first name, their last name, and their job title—and stitch it together into a single sentence.
Back in the day, we had to do something called "string concatenation." It looked like this:
var firstName = "Jane";
var lastName = "Doe";
var role = "Developer";
var greeting = "Hello, my name is " + firstName + " " + lastName + " and I am a " + role + ".";
Look closely at that last line. Look at the sheer number of quotation marks. Look at that empty space floating inside its own quotes " " just so the first and last name wouldn't smash together into JaneDoe.
If you missed a single space, or forgot a single + sign, the whole app would throw an error or the text would look completely broken on the screen. Writing code this way felt like performing delicate surgery with oven mitts on. Your brain had to constantly switch between "I am writing code" and "I am writing text." It was exhausting.
Then, ES6 came along and introduced Template Literals.
Honestly? It felt like stepping out of a cramped, stuffy room into a cool breeze. Let's talk about what they are, why they changed everything, and how to use them effectively in modern JavaScript.
The Magic Wand: The Backtick
The secret to template literals isn't a complex new keyword. It’s just a different type of quotation mark.
Instead of using single quotes (' ') or double quotes (" "), template literals use the backtick ( ).
(Side note: if you’ve never used a backtick before, it’s usually hiding in the top left corner of your keyboard, right under the Escape key, sharing a button with the tilde ~.)
Just by wrapping your text in backticks, you unlock superpowers that normal strings don't have.
Superpower #1: Embedding Variables (String Interpolation)
The biggest headache of the old way was stopping the string, adding a variable, and starting the string again.
With template literals, you don't have to stop. You just keep typing. When you want to drop a variable directly into your sentence, you wrap it in a dollar sign and curly braces, like this: ${variableName}.
This is called string interpolation. Let's rewrite that exact same greeting from earlier:
const firstName = "Jane";
const lastName = "Doe";
const role = "Developer";
const greeting = `Hello, my name is \({firstName} \){lastName} and I am a ${role}.`;
Isn't that just beautiful?
You just read it left to right, exactly the way a human speaks. The cognitive load drops to zero. You never have to worry about missing spaces or balancing plus signs again. The curly braces tell JavaScript, "Hey, pause the text for a second, figure out what this variable is, and print its value right here."
Superpower #2: Doing Math (and Logic) Inside the String
Here’s where it gets really fun. The space inside those curly braces ${...} isn’t just a dumb container for variables. It’s actually a mini JavaScript execution zone.
You can put any valid JavaScript expression inside of it.
Want to do some math? Go for it.
const price = 20;
const tax = 0.05;
const receipt = `Your total today is $${price + (price * tax)}.`;
// Output: Your total today is $21.
Want to run a quick logic check (like a ternary operator) to change the text based on a condition? You can do that too without breaking the string.
const isOnline = true;
const statusMessage = `User is currently ${isOnline ? 'Active' : 'Offline'}.`;
// Output: User is currently Active.
If we tried to do this with the old plus-sign concatenation, it would be a tangled, unreadable mess of brackets and quotes. Template literals keep the logic neatly contained right where the output actually lives.
Superpower #3: Multi-Line Strings (Without the Tears)
Let’s talk about building HTML blocks in JavaScript. Sometimes, you need to generate a chunk of HTML dynamically to inject into the DOM.
Before template literals, if you hit the "Enter" key in the middle of a standard string, JavaScript would panic and throw a syntax error. To make a string span multiple lines, you had to use the newline character \n and stitch the lines together with plus signs.
It looked like an absolute disaster:
// The Old Nightmare Way
var htmlTemplate =
"<div class='card'>\n" +
" <h2>" + user.name + "</h2>\n" +
" <p>" + user.bio + "</p>\n" +
"</div>";
Nobody wants to read that. Nobody wants to write that.
Template literals respect your Enter key. If you press Enter and start a new line, the template literal just says, "Okay cool, we're on a new line now." It preserves your line breaks and your indentation perfectly.
// The Modern, Clean Way
const htmlTemplate = `
<div class="card">
<h2>${user.name}</h2>
<p>${user.bio}</p>
</div>
`;
If you’ve ever written React, or used any modern component-based framework, this probably looks very familiar. While React's JSX isn't exactly a template literal under the hood, the philosophy of mixing logic and markup in a readable, multi-line format stems from this exact same desire for better developer ergonomics.
Real-World Use Cases in Modern JavaScript
You will use template literals every single day as a JavaScript developer. They are the standard now. Here are a couple of the most common places you'll find yourself reaching for the backtick:
1. Dynamic API Endpoints When fetching data from a server, you rarely hit a static URL. You usually need to request a specific resource by an ID.
const userId = 8675309;
// So much cleaner than 'https://api.myapp.com/users/' + userId + '/preferences'
const apiUrl = `https://api.myapp.com/users/${userId}/preferences`;
fetch(apiUrl)
.then(response => response.json())
.then(data => console.log(data));
2. Dynamic CSS Classes If you are toggling UI states (like changing a button from "idle" to "loading" to "success"), template literals make it incredibly easy to swap out class names on the fly.
const isError = true;
const buttonClass = `btn btn-primary ${isError ? 'btn-error' : ''}`;
A Quick Word on Readability
At the end of the day, code is written for computers to execute, but it is read by humans. You will spend ten times more time reading your code than you will spend writing it.
Every time you choose to use a template literal over old-school concatenation, you are doing a favor for your future self. You are removing a tiny layer of friction, making the code a little more self-documenting, and keeping your focus on the logic that actually matters—not on counting quotation marks.
So, if you still have any old strings in your codebase stitched together with + signs, do yourself a favor. Tear them down, wrap them in backticks, and enjoy the clean, readable beauty of modern JavaScript.





