JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Strings

javascript
junior
strings

What are template literals and tagged templates in JavaScript?

template-literals
strings
tagged-templates
es6
interpolation
Quick Answer

Template literals use backticks (`) instead of quotes, supporting string interpolation with ${expression}, multi-line strings, and embedded expressions. Tagged templates allow you to parse template literals with a custom function for advanced string processing.

Detailed Explanation

Template Literals (ES6) provide a cleaner way to create strings:

  1. String interpolation: Embed expressions with ${}
  2. Multi-line strings: Natural line breaks without \n
  3. Expression embedding: Any valid JS expression inside ${}

Tagged Templates are an advanced feature where a function processes the template literal:

  • The tag function receives the string parts and interpolated values separately
  • Used for: sanitization, internationalization, CSS-in-JS (styled-components), SQL queries

Common use cases:

  • Building HTML/JSX strings
  • Logging with dynamic values
  • URL construction
  • CSS-in-JS libraries

Code Examples

Template literals basicsJavaScript
const name = 'Alice';
const age = 25;

// String interpolation
const bio = `${name} is ${age} years old`;

// Multi-line strings
const html = `
<div class="card">
  <h2>${name}</h2>
  <p>Age: ${age}</p>
</div>
`;

// Expressions
const msg = `Total: $${(9.99 * 3).toFixed(2)}`; // 'Total: $29.97'
const status = `User is ${age >= 18 ? 'adult' : 'minor'}`;

// Nested template literals
const items = ['a', 'b', 'c'];
const list = `<ul>${items.map(i => `<li>${i}</li>`).join('')}</ul>`;

Real-World Applications

Use Cases

HTML Email Templates

Building dynamic email content with embedded variables and conditional sections using template literals

SQL Query Building

Using tagged templates to build parameterized queries that prevent SQL injection attacks

Log Formatting

Creating structured, readable log messages with interpolated context values and timestamps

Mini Projects

Mini Template Engine

beginner

Build a tagged template function that replaces placeholders with HTML-escaped values for safe rendering

HTML Email Builder

intermediate

Build a template system for generating styled HTML emails from structured data objects

Industry Examples

styled-components

Uses tagged template literals to define component styles with dynamic CSS and prop interpolation

graphql-tag

The gql tagged template parses GraphQL query strings into AST objects for use with Apollo Client

Lit

The html tagged template creates efficient, declarative templates that only re-render changed portions of the DOM

Resources

MDN - Template literals

docs

JavaScript.info - Template strings

article

Related Questions

What are the most commonly used string methods in JavaScript?

junior
strings
Previous
How does try/catch work in JavaScript and when should you use it?
Next
What is an IIFE (Immediately Invoked Function Expression) and why is it used?
PrevNext