JavaScript · Senior · Question 34 of 75

Why are eval and new Function usually banned by security policy?

Explanation

If user data ends up in the string, an attacker runs their own code. CSP blocks such calls by default. Also, eval hinders engine optimizations. There's almost always an alternative: JSON.parse, a function table, Function with code known at build time.

Further reading

Problem with a question

JavaScript · Senior · question 34

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz