is it evil to use eval to convert a string to a function? [closed]

i have heard lot’s of different opinions on eval()

eval is not evil in general, there are applications for it.

unsure if it’s ok to use eval() in this context

No, it is not OK for this. There is a much better solution using the simple bracket notation member operator. Since this is asked every day on SO, I can list hundreds of dupes here (just the first few Google results):

  • Convert string in dot notation to get the object reference
  • Convert javascript dot notation object to nested object
  • Accessing nested JavaScript objects with string key
  • access object through dot-syntax string path
  • Convert JavaScript string in dot notation into an object reference
  • Access object child properties using a dot notation string
  • Access property via it’s keyPath in Javascript?

Leave a Comment