As per the Mozilla docs in order to execute a function using eval
it must be wrapped inside (
)
i.e. if you don\'t use them then it treate
The documentation says nothing on function execution. The functions won't be executed, unless they are executed explicitly like (() => {})()
.
The quote
eval as a string defining function requires "(" and ")" as prefix and suffix
refers to interpreting the string as function expression rather than function declaration.
// will throw SyntaxError
// because function declaration requires a name
typeof eval('function (){}');
typeof eval('function a(){}') === 'undefined';
typeof eval('(function a(){})') === 'function';
typeof eval('null, function a(){}') === 'function';
typeof eval('()=>{}') === 'function';
Arrow function is always an expression, it doesn't need auxiliary constructions like comma or parentheses to interpret is an expression, here is the difference.