Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do string interpolation in javascript? [duplicate]

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?

like image 407
Tom Lehman Avatar asked Nov 22 '22 16:11

Tom Lehman


1 Answers

Since ES6, you can use template literals:

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

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

like image 191
Damian Pavlica Avatar answered Dec 20 '22 08:12

Damian Pavlica