When is JavaScript's eval() not evil?

Posted by Richard Turner on Stack Overflow See other posts from Stack Overflow or by Richard Turner
Published on 2008-10-13T14:28:28Z Indexed on 2010/04/13 20:53 UTC
Read the original article Hit count: 345

I'm writing some JavaScript to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval() on it to yield the result. However, I've always shied away from using eval() if I can avoid it because it's evil (and, rightly or wrongly, I've always thought it is even more evil in JavaScript because the code to be evaluated might be changed by the user).

Obviously one has to use eval() to parse JSON (I presume that JS libraries use eval() for this somewhere, even if they run the JSON through a regex check first), but when else, other than when manipulating JSON, it is OK to use eval()?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about coding-style