SKU // 30

JavaScript String Templates

Strings written with backticks ` instead of quotes have superpowers: you can drop variables and expressions straight into the text, and they can span multiple lines. They go by several names (template strings, template literals, string templates), all meaning the same thing. Added in ES6 (2015), they are now the standard way to build any string that includes values.

Finding the backtick
On most US keyboards the backtick ` is the key in the top-left corner, just below Esc, sharing a key with ~. It is not the apostrophe ' key.

// Backtick Syntax

let text = `Hello World!`;

On its own, that is just a normal string. The difference shows up in three places: quotes, line breaks, and ${ }.

// Quotes Inside Templates

Because the string is wrapped in backticks, single and double quotes inside it need no escaping at all:

let text = `He's often called "Johnny"`;
console.log(text);
He's often called "Johnny"

Only a backtick inside a template needs escaping: `Use the \` key`.

// Multi-Line Strings

With quotes, you need \n for a line break, and pressing Enter mid-string is an error. With backticks, just press Enter:

let text = `The quick
brown fox
jumps over
the lazy dog`;
console.log(text);
The quick brown fox jumps over the lazy dog
Watch out: indentation counts
Every character between the backticks is part of the string, including spaces. If you indent the second line to line it up with your code, those spaces end up in the text too.

// Interpolation: ${ }

This is the big one. Inside a template string, ${ } is a slot: JavaScript works out whatever is inside it and drops the result into the text. This is called interpolation.

let firstName = "John";
let lastName  = "Doe";
let text = `Welcome ${firstName}, ${lastName}!`;
console.log(text);
Welcome John, Doe!

Compare the same sentence built with +. The template version reads like the final sentence, with no juggling of quotes, spaces, and plus signs:

let name  = "Sam";
let score = 42;

let msg1 = "Player " + name + " scored " + score + " points.";
let msg2 = `Player ${name} scored ${score} points.`;

console.log(msg1);
console.log(msg2);
Player Sam scored 42 points. Player Sam scored 42 points.
Watch out: ${ } only works in backticks
With ordinary quotes, ${ } is just text. "Hello ${name}" logs exactly Hello ${name}, dollar sign and all. If your placeholders are showing up literally, check that the string uses backticks, not quotes.

// Expression Substitution

You are not limited to variable names. Any expression works inside ${ }: math, method calls, even a ternary.

let price = 10;
let VAT   = 0.25;
let total = `Total: ${(price * (1 + VAT)).toFixed(2)}`;
console.log(total);

let qty = 3;
console.log(`${qty} item${qty === 1 ? "" : "s"}`);
console.log(`Shouting: ${"hello".toUpperCase()}`);
Total: 12.50 3 items Shouting: HELLO

(toUpperCase() is a string method from the next lesson.) Remember the plural trick from the Ternary lesson, which needed extra parentheses when built with +? Inside ${ } the ternary is already neatly boxed off, so the precedence trap cannot happen.

Prices
In `$${price}` the first $ is just a dollar sign in the text, and the second starts the slot. `Cost: $${9.99}` gives Cost: $9.99.

// HTML Templates

Template strings are ideal for building chunks of HTML from data. Combined with a loop, they can turn a list into a web page list:

let header = "Template Strings";
let tags = ["template strings", "javascript", "es6"];

let html = `<h2>${header}</h2><ul>`;
for (const x of tags) {
  html += `<li>${x}</li>`;
}
html += `</ul>`;

console.log(html);
<h2>Template Strings</h2><ul><li>template strings</li><li>javascript</li><li>es6</li></ul>

Put that string into a page element with innerHTML (shown in Function Invocation) and the browser shows a heading and a bulleted list. Add a fourth tag to the array and the list grows automatically.

Security
Never drop text a user typed straight into HTML this way and put it on the page. Someone could type in a <script> tag, and the browser would run it. That attack is called cross-site scripting (XSS). Build HTML from your own data only, or clean user text first.

// Quick Check

What does this log?

let pet = "Rex";
console.log("Good dog, ${pet}!");
Show answer
Good dog, ${pet}!, exactly as written. The string uses double quotes, so ${ } is plain text. Swap the quotes for backticks to get Good dog, Rex!.

With let a = 4, b = 5;, which line logs 4 + 5 = 9?

  1. console.log(`${a} + ${b} = ${a + b}`);
  2. console.log(`a + b = a + b`);
  3. console.log(`${a + b} = ${a} + ${b}`);
  4. console.log("${a} + ${b} = ${a + b}");
Show answer
A. Each slot is worked out separately: 4, 5, and 9. B has no slots, so it logs the letters. C logs 9 = 4 + 5. D uses quotes, so the ${ } parts are just text.

Which is not a feature of template strings?

  1. Spanning multiple lines without \n
  2. Using both ' and " inside without escaping
  3. Automatically converting the text to uppercase
  4. Inserting expressions with ${ }
Show answer
C. That is what toUpperCase() is for. The other three are exactly what backticks add.

// Try It

  1. Click Open Playground. Rewrite the log as a template string and check it prints the same thing
  2. Print a receipt line showing an item, a quantity, and a total, using ${ } with math and $${ } for the price
  3. Use a ternary inside ${ } to print "1 message" or "5 messages"
  4. Build an HTML list from an array of three of your favorite songs using a loop and +=, then log it
// Video Walkthroughs
Last updated: Oct 9, 2026, 11:54 PM ET