cubetwo1729
cubetwo1729

Reputation: 1516

Why does this cause an "invalid syntax" error with Google's Closure Compiler?

If the following is passed into Google code closure:

return (function() {
    return true;
})();

it says there is a parsing error due to invalid syntax. What could be the problem?

Upvotes: 4

Views: 2756

Answers (2)

JaredPar
JaredPar

Reputation: 754525

The problem appears to be that you are using return as a top level construct (outside of any function body). You need to wrap it inside a context in which return is valid:

var example = function () {
  return (function() {
    return true;
  })();
};

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 992707

If that is your entire code, the problem is that you can't have a return statement (the first one) outside a function definition. Try:

function foo() {
    return (function() {
        return true;
    })();
}

Upvotes: 4

Related Questions