Why do people say that javascript eval() is evil but you get no objections against setTimeout and setInterval etc?

StackOverflow https://stackoverflow.com/questions/3492015

Question

if I am not mistaken eval executes valid code in a given string

eval("alert('hey')");

and

setTimeout("alert('hey')",1000);

does just about the same thing, only with a timer. is set timeout just as risky as eval?

Was it helpful?

Solution

I'd say you hear the same objections. setTimeout (with string and not function parameters) is pretty much the same as eval.

If possible,

 setTimeout(function(){ alert ("hey") ; }, 1000);

OTHER TIPS

Because when people say "eval", they mean "eval and any function that is more or less equivalent to eval", but the former is much shorter to say. So the answer to your question is yes, it is as risky.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top