Basic String Interpolation
Template strings use backticks (`) instead of quotes and allow you to embed variables using ${}:
Multi-line Strings
Template strings preserve line breaks, making them perfect for multi-line text:With template strings, you don’t need special escape characters like
\n for new lines - just write them naturally.Embedding Expressions
You can embed any JavaScript expression inside${}:
Advantages Over String Concatenation
Compare template strings with traditional concatenation:Common Use Cases
- Dynamic messages:
Welcome, ${username}! - URLs:
`https://api.example.com/users/${userId}` - HTML templates:
`<div class="user">${username}</div>` - Multi-line JSON: Creating formatted JSON strings