Back to Home
Strings

How to Interpolate Strings

Use template literals for string interpolation

Quick Answer (2024 ES6+ Way)

javascript
const name = 'World';
const greeting = `Hello, ${name}!`;
console.log(greeting); // "Hello, World!"

Live Example

javascript
const user = { firstName: 'Alice', age: 30 };
const message = `Welcome ${user.firstName}! You are ${user.age} years old.`;

console.log(message);

Common Variations

Multiline Strings
javascript
const html = `
  <div class="card">
    <h1>${title}</h1>
    <p>${content}</p>
  </div>
`;
With Expressions
javascript
const items = ['apple', 'banana', 'orange'];
const list = `Total items: ${items.length}, first: ${items[0]}`;
// "Total items: 3, first: apple"

❌ Don't Do This (Outdated Way)

Avoid string concatenation with + operator

javascript
// DON'T DO THIS
var greeting = 'Hello, ' + name + '!';

Browser Support

Works in all modern browsers (ES6+)

#template-literals#strings#interpolation