0

I need to mutate a strings quotes from double/single quotes to backticks, after it has already been created, so that I can add variables into the string.

I tried using replace(), but it seems as though JS does not evaluate the variable again after the replace method runs.

I am trying to understand if it is something that is possible to do, or variables can only be added to a string in JavaScript manually, and not dynamically?

Here is my code:

const variable = 9;

let strWithVar = "${variable}".replace(/"+/g, "`");

console.log(strWithVar);

The current return value is:

${abc}
Arcanus
  • 642
  • 5
  • 20

1 Answers1

-2

Maybe do:

var world = "World";
var hello = `Hello, ${world}`;
console.log(hello)
var bob = "bob"
var helloBob = `${hello}, ${bob}`
console.log(helloBob)
Ge1p
  • 35
  • 2
  • 7