+5

Why do we still learning eval() in JavaScript, here on Sololearn?

I saw this eval() related questions in a JavaScript challenge : var x = "2 * 2"; var y = 4; var z = eval( x + y) ; alert(z) // 48 as result When I tried this by my self on my pc. It says, EvalError: call to eval() blocked by CSP (Content Security Policy). I'm seeing this eval function too frequently in quizzes, My question is if this eval is depreciated by most of the modern browsers for the sake of security issues. Then what benefits we will get by learning it here. Even it is not practical in my opinion. Elaborate on this if I'm wrong.

12/1/2019 8:51:25 PM

🦋 KA 🐙

4 Answers

New Answer

+3

From the JavaScript fundamental I learn that eval() could be used for evaluation of the JavaScript statements from the strings. For example : var x = "12" // type of string eval(x) // returns 12 as type of number. var x = "alert('hello world')" ; eval(x) // calls an alert, says 'hello world' ; I don't see any benefits of it regarding anything else. And my question remains on its vulnerability issue is the same as it was. It would be more helpful if someone shows me how It would be helpful in the real world practices.

0

0

-1

Eval() maybe vulnerable so that's why you've got that error.. Eval is useful as well as important