Search code examples
javascriptstringconcatenation

How to use formatted strings instead of concatenation in JavaScript


I tried using ${} instead of the normal concatenation in JavaScript to make sentences after storing variables but it isn't running.

var firstName = "David";
var surname = "Israel";

console.log("My name is ${firstName} and my surname is ${surname}")

Solution

  • Try

    var surname = "Israel";
    
    console.log(`My name is ${firstName} and my surname is ${surname}`)