How can I do string interpolation in JavaScript?

Tom Lehman picture Tom Lehman · Sep 11, 2009 · Viewed 378.3k times · Source

Consider this code:

var age = 3;

console.log("I'm " + age + " years old!");

Are there any other ways to insert the value of a variable in to a string, apart from string concatenation?

Answer

Damjan Pavlica picture Damjan Pavlica · Mar 14, 2016

Since ES6, you can use template literals:

const age = 3
console.log(`I'm ${age} years old!`)

P.S. Note the use of backticks: ``.