0

I am doing :

eval('function(){ console.log("Hello World"); }')();

But that gives error :

Uncaught SyntaxError: Unexpected token (

Why is this wrong?

1
  • Worksforme in Opera's console. How do you test it, in which environment? Commented Aug 21, 2014 at 12:48

3 Answers 3

3

The eval operator expects a Program as input, and the grammar of JavaScript requires that all top-level program elements are either declarations or statements.

The spec says:

Let prog be the ECMAScript code that is the result of parsing x as a Program.

function can't start a top-level statement, but it can start a function declaration, but only when it has a name.

That's why you get "Unexpected token ("; it expects a function name before the parenthesis that opens the argument list.

As others have noted, to eval a function, you need to trick the JavaScript parser into finding an expression where it expects a statement. Wrapping the body in parentheses is one way to do this.

Sign up to request clarification or add additional context in comments.

4 Comments

Not if you're passing a self-invoking function, which I believe is what he is attempting to do (due to his dubious added parenthesis on the end of the eval method)
@shennan, I'm not sure I understand. eval does what it does. The string the OP is passing in is a valid function expression, but is not a valid expression statement.
I agree with you. What I'm saying is, his added parenthesis at the end of his eval method (please take another look at it), is indication that perhaps he was attempting an anonymous function call, but misconstrued as to where the required extra parenthesis should be.
@shennan, I think we agree.
0

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.

Comments

0

What you want is this:

eval( '(function(){ console.log("Hello World"); })()' );

Let's break it down. You have a self invoking function (a function that calls itself):

(function(){ console.log("Hello World"); })()

And you're passing it as a string argument to the eval method:

eval( '(function(){ console.log("Hello World"); })()' );

Your main error is trying to call what eval returns by adding parenthesis on the end of the eval method call. If you want the function your passing to immediately invoke itself, the added parenthesis should be part of the thing your passing to eval.

I'm assuming you're trying to do something other than call "Hello World", as you could simply do this:

eval( 'console.log("Hello World");' );

Or, dare I say:

console.log("Hello World");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.