🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Template Literals

AI & DATA SCIENCE // template-literals

Template literals (backtick strings) support multi-line strings, expression interpolation, and tagged templates.

Syntax

const name = 'Alice';
const msg = `Hello, ${name}!`;    // interpolation
const multi = `Line 1
Line 2`;  // multi-line

Deep Dive Course

Template literals use **backticks** (`` ` ``) and support: **interpolation** `${expression}` (any JS expression), **multi-line strings** (without `\n`), and **tagged templates** (a function called with the template's parts). They replace string concatenation with `+`.

1Understanding Template Literals

Template literals use backticks (` `) and support: **interpolation** ${expression} (any JS expression), **multi-line strings** (without \n), and **tagged templates** (a function called with the template's parts). They replace string concatenation with +`.

💡

You can put any expression inside ${}: ${user.age >= 18 ? 'adult' : 'minor'} or ${arr.map(x => x * 2).join(', ')}.

editor.html
const user = { name: 'Alice', age: 30 };

// Interpolation
console.log(`Name: ${user.name}, Age: ${user.age}`);

// Expression inside interpolation
console.log(`Status: ${user.age >= 18 ? 'adult' : 'minor'}`);

// Multi-line
const html = `
  <div class="card">
    <h2>${user.name}</h2>
  </div>
`;
localhost:3000

2Practical Example

Here is a real-world application of Template Literals showing how it is used in production JavaScript code.

editor.html
// Tagged template literal
function highlight(strings, ...values) {
  return strings.reduce((result, str, i) => {
    const val = values[i - 1];
    return result + `<mark>${val}</mark>` + str;
  });
}

const price = 42;
const msg = highlight`The price is ${price} dollars`;
console.log(msg); // The price is <mark>42</mark> dollars
localhost:3000

3Best Practices

Follow these guidelines when working with Template Literals:

1. Use template literals instead of string concatenation

2. Use for multi-line HTML templates

3. Use tagged templates for SQL query sanitization, i18n, styled-components

⚠️

Tip: You can put any expression inside ${}: ${user.age >= 18 ? 'adult' : 'minor'} or ${arr.map(x => x * 2).join(', ')}.

editor.html
const user = { name: 'Alice', age: 30 };

// Interpolation
console.log(`Name: ${user.name}, Age: ${user.age}`);

// Expression inside interpolation
console.log(`Status: ${user.age >= 18 ? 'adult' : 'minor'}`);

// Multi-line
const html = `
  <div class="card">
    <h2>${user.name}</h2>
  </div>
`;
localhost:3000

Examples

Example 01Basic Usage
const user = { name: 'Alice', age: 30 };

// Interpolation
console.log(`Name: ${user.name}, Age: ${user.age}`);

// Expression inside interpolation
console.log(`Status: ${user.age >= 18 ? 'adult' : 'minor'}`);

// Multi-line
const html = `
  <div class="card">
    <h2>${user.name}</h2>
  </div>
`;
Example 02Advanced Example
// Tagged template literal
function highlight(strings, ...values) {
  return strings.reduce((result, str, i) => {
    const val = values[i - 1];
    return result + `<mark>${val}</mark>` + str;
  });
}

const price = 42;
const msg = highlight`The price is ${price} dollars`;
console.log(msg); // The price is <mark>42</mark> dollars

Best Practices

  • Use template literals instead of string concatenation
  • Use for multi-line HTML templates
  • Use tagged templates for SQL query sanitization, i18n, styled-components

Interview Question

What are tagged templates and what are they used for?

Hint: A function processes the template parts.

Tagged templates call a function (the tag) with the string parts and interpolated values. The tag function can process them arbitrarily — this powers: styled-components (CSS-in-JS), GraphQL query syntax (gql``), SQL sanitization, i18n/localization, and custom string formatting. They're one of JavaScript's most powerful features.

Exercises

MediumPractice using Template Literals in a real scenario.
View Solution
const user = { name: 'Alice', age: 30 };

// Interpolation
console.log(`Name: ${user.name}, Age: ${user.age}`);

// Expression inside interpolation
console.log(`Status: ${user.age >= 18 ? 'adult' : 'minor'}`);

// Multi-line
const html = `
  <div class="card">
    <h2>${user.name}</h2>
  </div>
`;

Frequently Asked Questions

What are tagged templates and what are they used for?

Tagged templates call a function (the tag) with the string parts and interpolated values. The tag function can process them arbitrarily — this powers: styled-components (CSS-in-JS), GraphQL query syntax (gql``), SQL sanitization, i18n/localization, and custom string formatting. They're one of JavaScript's most powerful features.

Related Functions

VariablesDestructuringSpread Operator