Skip to main content

Command Palette

Search for a command to run...

Template Literals in JavaScript

Published
4 min read
Template Literals in JavaScript
A
my work defines me

Have you ever felt like you were doing more "math" with plus signs and quotes than actually writing text in your JavaScript code? Writing dynamic sentences used to feel like piecing together a broken vase one wrong quote and the whole thing falls apart.

Template Literals changed the game, making string manipulation feel less like a chore and more like writing a simple message.

Here is what we’ll cover in this guide:

  • The "Headache" of traditional string concatenation

  • The Backtick (`) syntax

  • Embedding variables (Interpolation)

  • Writing multi-line strings without the mess

  • Real-world use cases


The Problems with Traditional Concatenation

Before Template Literals (ES6), we used single (') or double (") quotes and the plus (+) operator to join strings and variables.

The Pain Points:

  • Quote Confusion: Mixing single and double quotes often led to syntax errors.

  • Spacing Issues: It was incredibly easy to forget a space between a word and a variable, resulting in HelloAbhinavinstead of Hello Abhinav.

  • Readability: As soon as you had more than two variables, the code became a wall of quotes and plus signs.


Template Literal Syntax

The biggest change is the character used to wrap the string. Instead of quotes, we use backticks (`), usually found right below the Esc key on your keyboard.

// The old way
const message = "Hello, world!";

// The new way
const message = `Hello, world!`;

On its own, it looks the same. The magic happens when we start adding dynamic data.


Embedding Variables (Interpolation)

In the old days, adding a variable looked like this: "Hello " + name + ", welcome back!"

With Template Literals, we use Interpolation. You simply wrap your variable or expression in ${ } inside the backticks. JavaScript will "calculate" whatever is inside those braces and turn it into a string.

Comparision:

const name = "Abhinav";
const items = 5;

// Old Way (Messy)
console.log("User " + name + " has " + items + " items in their cart.");

// New Way (Clean)
console.log(`User \({name} has \){items} items in their cart.`);

It reads like a normal sentence, making it much harder to make mistakes.


Multi-line Strings

If you wanted to start a new line in a traditional string, you had to use the special \n character. If you actually pressed "Enter" in your code editor, the code would break.

Template Literals are whitespace sensitive. This means if you hit Enter inside the backticks, that new line is preserved in the output.

Example:

// The Old way (Ugly)
const emailBody = "Hello,\n\n" + 
                  "Your order is ready.\n" + 
                  "Thanks!";

// The New way (Beautiful)
const emailBody = `Hello,

Your order is ready.
Thanks!`;

Use Cases in Modern JavaScript

Why should you use these every day?

  • HTML Templates: If you are injecting HTML into a webpage using JS, template literals allow you to write the HTML exactly as it looks.

  • Dynamic Logging: Creating descriptive console logs or error messages becomes a breeze.

  • Mathematical Expressions: You can actually do math inside the braces: Total: ${price * tax}.


Conclusion

emplate Literals are a perfect example of how JavaScript has evolved to be more "human-friendly." They remove the clutter of concatenation and let you focus on the logic of your code. If you aren't using them yet, try replacing one + joined string in your current project with backticks you’ll notice the difference in readability instantly.