JavaScript Template Literals

Strings With Built-in Variables

JavaScript Template Literals

Template literals use backticks and let you drop variables right into the middle of a string. Multi-line strings, too.

4 min read Level 1/5 #strings#template-literals#interpolation
What you'll learn
  • Write strings with backticks
  • Interpolate variables with `${ }`
  • Write multi-line strings without `\n`

A template literal is a string wrapped in backticks (`) instead of quotes. Two extra superpowers:

  1. Interpolation: drop variables inside with ${ }.
  2. Multi-line: line breaks in your source become line breaks in the string.

Interpolation

The most common reason to use template literals.

Variable interpolation script.js
const name = "Ada";
const age = 36;

const greeting = `Hello, ${name}! You are ${age} years old.`;
console.log(greeting);
// "Hello, Ada! You are 36 years old."
▶ Preview: console

Compare with the older approach using +:

// Old:
const greeting = "Hello, " + name + "! You are " + age + " years old.";

// Template literal:
const greeting = `Hello, ${name}! You are ${age} years old.`;

The template literal version reads more like the final output. No quote juggling, no + everywhere.

Expressions, Not Just Variables

Anything inside ${ } is a full expression — math, function calls, property access, conditionals.

Expressions in template literals script.js
const price = 99;
const quantity = 3;

console.log(`Subtotal: ${price * quantity}`);          // "Subtotal: 297"
console.log(`Discounted: ${(price * 0.9).toFixed(2)}`); // "Discounted: 89.10"
console.log(`In stock: ${quantity > 0 ? "yes" : "no"}`); // "In stock: yes"
▶ Preview: console

Multi-Line Strings

Backticks span line breaks. Whatever you write across multiple lines in the source goes into the string as-is.

Multi-line strings script.js
const message = `Dear Ada,

Thank you for using jsschools.

Best,
The team`;

console.log(message);
▶ Preview: console

When to Use Which

  • Single quotes ('...') — short plain strings.
  • Double quotes ("...") — same, used in JSON.
  • Backticks (`...`) — anytime you have a variable inside, or your string spans multiple lines.

Many teams use backticks always for consistency. Either approach is fine — pick a rule and follow it.

Tagged Templates (Preview)

You can also “tag” a template literal with a function. That function receives the parts and the values, and can do anything with them.

function highlight(strings, ...values) {
  return strings.map((s, i) => s + (values[i] ? `**${values[i]}**` : "")).join("");
}

const name = "Ada";
console.log(highlight`Hello, ${name}!`);
// "Hello, **Ada**!"

Tagged templates power some libraries (CSS-in-JS, SQL builders). You’ll rarely write one yourself when starting out — just know they exist.

Try It Yourself

Exercise

Build a greeting

Difficulty 1/5~2 min
Given `const name = "Ada"` and `const age = 36`, build a constant `greeting` (using a template literal) that equals `"Hello, Ada! You are 36 years old."`. Log it.
solution.js
const name = "Ada";
const age = 36;
// your code here
2tests will run
💡 Show hint
Use backticks and `${ }` to insert `name` and `age` into the string.
✅ Show solution
const name = "Ada";
const age = 36;
const greeting = `Hello, ${name}! You are ${age} years old.`;
console.log(greeting);

Up Next

You’ve seen strings inside and out. Now: numbers.

JavaScript Numbers →