Last Updated: February 25, 2016
· ernestocodesnippets

eval() is evil

Yep eval() is evil, but what if we need to evaluate code from a string, and there is no other workaround?
Use a temporary function, like in the example:

var string = 'alert("eval is evil")';

// bad

// good
var tmpFunc = new Function(string);

// **same** result, less work for the interpreter, faster code, and more maintainable

Link to the example at jsFiddle:

An article on eval():