Back to JavaScript Notes
Topic #296

String Templates

You will be able to build readable JavaScript strings by embedding variables and expressions inside template literals.

What it is

A template literal is a string written with backticks instead of single or double quotes. Inside it, you can embed JavaScript expressions using ${expression}. The expression is evaluated, converted to a string, and inserted into the final string. Template literals also preserve line breaks and indentation, which makes multi-line text easier to write.

Related terms: interpolation, template string, tagged template literal, and expression.

Why it matters

  • It reduces noisy + concatenation.
  • It keeps dynamic values close to the surrounding text.
  • It supports expressions, not just simple variables.
  • It makes multi-line strings readable.
  • It works well for messages, URLs, queries, and HTML snippets.

Syntax or steps

  1. Open and close the string with backticks: `...`.
  2. Write normal text where needed.
  3. Insert an expression between ${ and }.
  4. Use any valid expression: variable, function call, ternary, arithmetic, or object property.
const user = { name: "Ada" };
const greeting = `Hello, ${user.name}!`;

Example

const user = { name: "Ada", role: "admin", unread: 3 };
const status = user.unread > 0 ? "needs attention" : "clear";

const message = `User: ${user.name}
Role: ${user.role}
Unread messages: ${user.unread}
Status: ${status}`;

console.log(message);
  • user is an object with properties used in the template.
  • status is computed before the string is built.
  • ${user.name} inserts the value of user.name.
  • ${user.unread} inserts a number, which becomes text in the string.
  • The line breaks inside the backticks are kept in the final string.

Common mistakes

  • Using quotes instead of backticks. "Hi, ${name}!" does not interpolate.
  • Forgetting the dollar sign. `Hi, {name}!` prints literal braces.
  • Putting statements inside ${}. Only expressions are allowed.
  • Assuming objects print usefully. ${user} becomes [object Object] unless you choose a property or convert it.

When to use it

Use template literals when a string contains dynamic values or spans multiple lines. Use normal quoted strings when the text is fixed and simple.

ApproachBest forExample
Template literalInterpolated or multi-line text`Hi, ${name}!`
Quoted stringFixed, simple text"Hi!"
ConcatenationOlder code or very short joins"Hi, " + name + "!"

Practice

Guided exercise: Create variables orderId and customer, then build a template literal that outputs Order #42 shipped to Sam.

Challenge: Add a total variable and include a conditional expression that says free shipping when the total is at least 50, or shipping applies otherwise.

Hint: Use ${total >= 50 ? "free shipping" : "shipping applies"} inside the template literal.

Quick check

What does `Total: ${1 + 2}` produce?

Answer: Total: 3

Summary

Template literals let you write strings as text with embedded expressions. They make JavaScript code easier to read when values change or when you need multi-line output. Use them instead of concatenation whenever the string is dynamic.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

String Templates – FAQs

Quick answers about learning String Templates in JavaScript.

This free note from Coding Now Tech Institute explains String Templates in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including String Templates, is 100% free with no signup required.
With focused practice, most students grasp String Templates in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now