How can I interpolate a JavaScript variable in string without using concatenation?

How can I interpolate a JavaScript variable in string without using concatenation?

In PHP, you can do this easily, like $my_string = “I pity the $hello”;, but is there a similar way to use variables inside strings in JavaScript for a cleaner approach?

Hi,

The most elegant and modern way to interpolate a javascript variable in string is by using template literals, which are enclosed in backticks (`). You can embed variables directly using ${}.

let hello = "foo";
let myString = `I pity the ${hello}`;
console.log(myString); // Output: "I pity the foo"

This approach is concise and makes your code more readable.