1

Is it possible to create a template literal from a normal string? e.g. from a definition in some JSON.

var foo = 'world';
var string = "hello ${foo}";
var string2 = `${string}`; // "hello ${foo}" - but I want "hello world"
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • You string has `"hello ${foo}"` cause you didn't used the backticks to enclose `string`. It will be treated as normal string. Enclose them in back tick – Prabodh M Jun 24 '17 at 09:35
  • 1
    Yes - that is the crux of my question. My best guess currently would be to use `eval` and include the backtracks in the JSON. – Ben Aston Jun 24 '17 at 09:38
  • Ahh! I see, but if you include backticks inside a string, it will be considered part of string, it won't be interpreted as template literal. – Prabodh M Jun 24 '17 at 09:45

0 Answers0