Skip to content

Template Literals

This lesson explains Template Literals in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.

JavaScript Template Literals Overview

JavaScript template literals are an ES6 feature that allows developers to create strings using backticks instead of single or double quotes. Template literals make it easier to write multi-line strings, insert variables inside strings, and build dynamic text in a cleaner and more readable way.

Template literals are commonly used in frontend development, API messages, dynamic HTML generation, logging, email templates, URL creation, React-style string formatting, and Node.js applications.

Feature Description Common Usage
Backtick Strings Creates strings using backticks. Cleaner string syntax.
String Interpolation Inserts variables or expressions into strings. Dynamic messages.
Multi-line Strings Allows strings across multiple lines. Templates and formatted text.
Expression Support Evaluates JavaScript expressions inside strings. Calculations and conditions.
Tagged Templates Processes template literals with a function. Advanced formatting and libraries.

JavaScript Template Literals Example

const name = "John";

const role = "Developer";

const message = `Hello ${name}, welcome to JavaScript.`;

const profile = `
Name: ${name}
Role: ${role}
`;

console.log(message);
console.log(profile);

This example shows how template literals use backticks and ${expression} syntax to insert variables into strings and create multi-line text.

Top 10 JavaScript Template Literal Examples

The following examples show common template literal patterns used in modern JavaScript applications for dynamic strings, messages, URLs, HTML, logging, and formatting.

# Example Syntax
1 Insert Variable `Hello ${name}`
2 Insert Expression `Total: ${price + tax}`
3 Multi-line String `Line one Line two`
4 Create URL `/users/${userId}/profile`
5 Dynamic CSS Class `card ${isActive ? "active" : ""}`
6 Format Price `Price: $${amount}`
7 HTML Template `<h2>${title}</h2>`
8 Log Message `User ${name} logged in`
9 Function Result `Today is ${getDay()}`
10 Tagged Template format`Hello ${name}`

Best Practices

  • Use template literals when strings include variables or expressions.
  • Use template literals for multi-line strings instead of string concatenation.
  • Keep embedded expressions simple and readable.
  • Avoid building unsafe HTML with untrusted user input.
  • Use helper functions when template logic becomes complex.
  • Escape or sanitize user data before inserting it into HTML strings.
  • Use template literals for readable URLs, messages, and logs.
  • Avoid mixing too much business logic inside template strings.

Common Mistakes

  • Using single or double quotes instead of backticks.
  • Forgetting the ${} syntax for interpolation.
  • Putting complex logic directly inside a template literal.
  • Using template literals to insert unsafe user input into HTML.
  • Confusing template literals with regular strings.
  • Forgetting that whitespace and line breaks are preserved.
  • Overusing template literals when a normal string is simpler.

Key Takeaways

  • Template literals use backticks instead of quotes.
  • String interpolation uses ${expression}.
  • Template literals support multi-line strings.
  • They make dynamic strings easier to read than concatenation.
  • They are commonly used for URLs, messages, HTML snippets, and logs.
  • Always be careful when inserting user input into HTML templates.

Pro Tip

Use template literals when building dynamic strings, but keep expressions simple. If the template becomes hard to read, move the logic into a separate variable or helper function.