How Can I Do String Interpolation in Javascript?
Consider This Code: Var Age = 3; Console. Log("I'm " + Age + " Year's Old!"); Are There Any Other Ways to Insert the Value of a Variable in to a String, Apart...
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?
21 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: ``.
Must Read
tl;dr
Use ECMAScript 2015's Template String Literals, if applicable.