-7

say i have a string like this

const myObj = {"far": " bar"}
const s = "hello " + JSON.stringify(myObj)

How to convert it to template literal ?

JJJ
  • 32,902
  • 20
  • 89
  • 102
user3552178
  • 2,719
  • 8
  • 40
  • 67
  • 1
    Please read the documentation: [Template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) – adiga Aug 26 '19 at 19:43
  • Possible duplicate of [Convert a string to a template string](https://stackoverflow.com/questions/29182244/convert-a-string-to-a-template-string) – Omri Attiya Aug 26 '19 at 19:44
  • What function are you trying to call? Please show exactly what Javascript you're trying to execute? The string you show does not show any function or code for a function call. In general, it's bad form to construct a string in Javascript and then try to execute it. In general, you already have the function so you just parse the parameters from the string and make a normal function call in regular Javascript. – jfriend00 Aug 26 '19 at 19:51
  • @jfriend00 i thought JSON.stringify() is a function – user3552178 Aug 26 '19 at 20:15

1 Answers1

1

The same way you put any other expression in a template literal, just wrap it in ${}.

const myObj = {"far": " bar"}
const s = `hello ${JSON.stringify(myObj)}`;
console.log(s);
Barmar
  • 741,623
  • 53
  • 500
  • 612