JavaScript Template Literals
JavaScript template literals use backtick characters to simplify string creation by allowing embedded variables, expressions, and multi-line text. This post covers the core syntax, practical examples, and common mistakes beginners should avoid.
If you've ever tried to build a string in JavaScript by gluing together variables and text with plus signs, you know how messy it can get. JavaScript template literals solve this problem elegantly, and once you start using them, you won't want to go back.
What Are Template Literals?
Template literals are a way to create strings in JavaScript using backtick characters (`) instead of single or double quotes. They were introduced in ES6 (also called ES2015) and bring three major improvements to string handling: easier variable embedding, multi-line strings, and expression evaluation directly inside strings.
Here's the simplest comparison. The old way:
const name = "Alex";
const greeting = "Hello, " + name + "! Welcome back.";With a template literal:
const name = "Alex";
const greeting = `Hello, ${name}! Welcome back.`;Both produce the same result, but the template literal version is far easier to read and maintain.
Embedding Variables and Expressions
The ${} syntax is called an expression placeholder. Anything inside the curly braces is evaluated as JavaScript. This means you're not limited to just variable names; you can run actual code inside a string.
const price = 49.99;
const quantity = 3;
const summary = `You ordered ${quantity} items at $${price} each.
Total: $${(price * quantity).toFixed(2)}`;
console.log(summary);
// You ordered 3 items at $49.99 each.
// Total: $149.97Notice the math happening directly inside the string. You can call functions, use ternary operators, access object properties, and more, all within ${}.
Multi-Line Strings
Before template literals, creating a multi-line string required awkward workarounds using \n escape characters or string concatenation across multiple lines. Template literals handle this naturally:
const message = `Dear Customer,
Thank you for your recent order.
We will process it within 2 business days.
Best regards,
The Support Team`;
console.log(message);Line breaks you type inside backticks are preserved exactly as written. This is especially useful when building HTML snippets, email templates, or multi-line log messages in your scripts.
Expressions Inside Placeholders
Here's a practical example using a ternary expression to conditionally change the output of a string:
const score = 72;
const result = `You scored ${score}/100. You have ${score >= 60 ? "passed" : "failed"} the test.`;
console.log(result);
// You scored 72/100. You have passed the test.This kind of inline logic keeps your code concise without needing separate variables just to build a string.
Using Template Literals with Objects
When working with objects, template literals make data presentation clean and straightforward:
const user = {
username: "jsmith",
role: "admin",
lastLogin: "2025-06-01"
};
const report = `User Report
-----------
Username : ${user.username}
Role : ${user.role}
Last Login: ${user.lastLogin}`;
console.log(report);This pattern is common in logging, debugging output, and building dynamic content in web applications.
A Note on Tagged Templates
Template literals have an advanced feature called tagged templates, where you can prefix a template literal with a function name to process the string. This is used in libraries like styled-components and GraphQL tools. It's worth knowing they exist, but as a beginner, focus on the basics first. Tagged templates will make more sense once you're comfortable with functions and array methods.
Common Mistakes to Avoid
- Forgetting backticks: Template literals require the backtick character (
`), not single or double quotes. - Misusing curly braces: The placeholder syntax is
${expression}. Curly braces alone, or dollar sign alone, won't trigger substitution. - Nesting quotes carelessly: You can freely use single and double quotes inside template literals without escaping them, which is actually a bonus.
What's Next
Now that you can build strings cleanly with template literals, the next logical step is understanding how JavaScript handles data more broadly. In the next post, we'll look at JavaScript arrays, including how to create them, access their elements, and use built-in methods to manipulate data. Arrays and strings often work together, so this foundation will serve you well.