Beginner 5 min

Strings and template literals

A template literal lets you include values directly inside a string.

How it works

Write the sentence first, then replace just the changing parts with placeholders. Before displaying it, JavaScript evaluates each placeholder and inserts its text form. A number like 2 can therefore sit beside a name without manually joining three strings.

Walk through an example

Use backticks around Hi ${name}. JavaScript evaluates the expression inside the braces and inserts its result into the text.

What to watch for

Single or double quotes do not interpolate ${...} expressions. For predictable output, decide how numbers and missing values should be displayed.

Code example

const name = 'Mira';
const count = 2;
console.log(`${name} has ${count} books.`);

Check your understanding

What does the example greeting display?

See the example and explanation
Worked example
Given name = 'Mira' and count = 2
Answer Mira has 2 books.

The template inserts Mira at the first placeholder and 2 at the second.

Open practice problem
Next lessonBranches and loops