Insert Values
String interpolation inserts variables or expressions into a string.
In JavaScript, interpolation is done with template literals.
JavaScript Tutorial
String interpolation inserts values directly into a string using ${}.
It is cleaner than concatenation for complex strings.
UI strings often combine text and data. Interpolation keeps them readable.
It reduces mistakes compared to multiple + operators.
`Hello ${name}`
`Total: ${price + tax}`const name = 'Ava';
const msg = `Hello ${name}`;
console.log(msg);Insert a variable into a string.
const price = 100;
const tax = 0.18;
const total = `Total: ${price + price * tax}`;
console.log(total);Use expressions inside ${}.
String interpolation inserts variables or expressions into a string.
In JavaScript, interpolation is done with template literals.
Interpolation avoids + concatenation and keeps strings readable.
It reduces bugs in long strings and UI templates.
You can embed any expression inside ${}.
This includes math, function calls, or ternaries.
const isMember = true;
const label = `Price: ${isMember ? 'Member' : 'Standard'}`;
console.log(label);Ternaries work inside interpolation.
function format(n) { return n.toFixed(2); }
const value = `Amount: ${format(12.3)}`;
console.log(value);Call functions inside templates.
Without
const msg = "Hello " + name + "!";With
const msg = `Hello ${name}!`;Interpolation only works with backticks.
Use ${expression}, not {expression}.
Move complex logic into variables or functions.
Template literals with backticks.
Yes, any expression inside ${}.
Move it into a variable or function first.
Practice: Build a message that shows username and item count.
const user = 'Ava';
const count = 3;
// TODO: create message
One Possible Solution
const user = 'Ava';
const count = 3;
const msg = `User ${user} has ${count} items`;
console.log(msg);Use template literals with ${} expressions.
Yes, any valid expression works.
Performance is similar; choose readability.
Try different expressions inside ${}.