I am doing :
eval(\'function(){ console.log(\"Hello World\"); }\')();
But that gives error :
Uncaught SyntaxError: Unexpected t
eval
expects a statement but:
function(){}
is not valid as a statement because the function name is missing.
(function(){})
is instead recognized because the statement is a "statement expression".
The problem is that function
as first token triggers the function declaration rule when a statement is expected. When an expression is expected instead (e.g. inside parenthesis) then function
triggers the function expression rules where a name is optional but not mandatory.