In JavaScript, eval
is considered to be a bad idea mainly because it opens up your code to injection attacks. To quote this comment:
The attacks we are trying to avoid are when user provided values get saved, then later placed into javascript and eval'd. For example, I might set my username to:
badHackerGuy'); doMaliciousThings();
and if you take my username, concat it into some script and eval it in other people's browsers then I can run any javascript I want on their machines (e.g. force them to +1 my posts, post their data to my server, etc.)
But why is this problem limited to JavaScript? Yes, JavaScript is the only language that runs on browsers, but there are contexts other than browsers where you want to avoid injection attacks.
(This part is tangential to my actual question, but people talk about the extreme, awesome power of metaprogramming. eval
allows you to do metaprogramming and it sounds like the injection attack issue is the only big downside. Why is considered to be clear that the downside outweighs the upside?)