JavaScript string interpolation

concatenating strings with variables in a neater arrangement

// updated 2025-05-11 11:40

In JavaScript (ES6 and later), we can use string interpolation when we want to concatenate strings with variables in a neater arrangement...

Instead of using plus (+) signs and a bunch of quotation marks:

1var x = 'something'
2var y = 'another thing'
3var z = 'yet another thing'
4
5var sentence = 'We would like' + x + 'and then' + y + 'and also' + z + '!';

We could use a different syntax:

1const x = 'something'
2const y = 'another thing'
3const z = 'yet another thing'
4
5const sentence = `We would like ${x} and then ${y} and also ${z}!`

Note that:

  • we use backtick symbols (`) to encapsulate the total string
  • we use { curly braces } around the variable names
⬅️ older (in textbook-javascript)
📒 JavaScript escape sequences
newer (in textbook-javascript) ➡️
JavaScript ternary operators 📒
⬅️ older (in code)
📒 JavaScript escape sequences
newer (in code) ➡️
JavaScript ternary operators 📒
⬅️ older (posts)
📒 JavaScript escape sequences
newer (posts) ➡️
JavaScript ternary operators 📒