Backtick Strings
Template literals use backticks (`) instead of quotes.
They make multi-line strings and interpolation easy.
JavaScript Tutorial
Template literals use backticks and allow interpolation with ${}.
They make string formatting cleaner and more readable.
String concatenation can get messy with multiple variables.
Template literals keep formatting clear and maintainable.
`Hello ${name}`
`Line1
Line2`const name = 'Ava';
const msg = `Hello ${name}`;
console.log(msg);Interpolate variables with ${}.
const text = `Line one
Line two`;
console.log(text);Template literals preserve line breaks.
Template literals use backticks (`) instead of quotes.
They make multi-line strings and interpolation easy.
Use ${} to insert variables or expressions into strings.
This is cleaner than string concatenation.
Template literals can be tagged to process strings in custom ways.
This is advanced but useful for localization or formatting.
const a = 2;
const b = 3;
const sum = `Total: ${a + b}`;
console.log(sum);You can place any expression inside ${}.
function tag(strings, value) {
return strings[0] + value.toUpperCase();
}
const result = tag`Hi ${'ava'}`;
console.log(result);Tagged templates give you control over formatting.
Without
const msg = "Hello " + name + ", you have " + count + " items";With
const msg = `Hello ${name}, you have ${count} items`;Interpolation only works in backtick strings.
Use ${expression}, not $expression.
Use tags only when you need custom formatting.
Backticks (`).
Use ${variable} inside the template literal.
A function that processes a template literal.
Practice: Create a template literal that shows a user's name and age.
const name = 'Ava';
const age = 22;
// TODO: create message
One Possible Solution
const name = 'Ava';
const age = 22;
const msg = `Name: ${name}, Age: ${age}`;
console.log(msg);They allow interpolation and multi-line content.
Yes, any expression inside ${} is allowed.
Performance is similar; choose readability.
Try interpolating different values.