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:
var x = 'something'
var y = 'another thing'
var z = 'yet another thing'
var sentence = 'We would like' + x + 'and then' + y + 'and also' + z + '!';
We could use a different syntax:
const x = 'something'
const y = 'another thing'
const z = 'yet another thing'
const 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