3

I know the basic working of JavaScript template literals.

var name = 'nithin'
console.log(`hello ${name}`) 
//this will print "hello nithin"

What if my template literal is stored in a DB and I'm dynamically querying the DB to get the corresponding template literal? How can I evaluate this?

This is my scenario:

I have store the template literal in DB as a string since I can't store it with backticks:

"welcome ${user}"

And I'm querying the DB for the string. Suppose I have this string in the variable stringFromDB. Now I want to evaluate this string using template literal method.

I have tried the following, but it isn't working:

eval(`stringFromDb`)

I have a variable named user in the same scope which is having a value "nithin". Im expecting "welcome nithin" as result. Any help?

whostolemyhat
  • 3,107
  • 4
  • 34
  • 50
nithin ks
  • 285
  • 3
  • 11

0 Answers0