Evaluate a string of JavaScript statements
suggest changevar x = 5;
var str = "if (x == 5) {console.log('z is 42'); z = 42;} else z = 0; ";
console.log("z is ", eval(str));
In non-strict code this eval can create a global z, returns 42, and also runs the console.log inside the string. eval is strongly discouraged. See the remarks on eval.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents