2

I'm just in the console in Chrome for now. I created this variable:

url = window.location.href

which gives:

url "http://www.example.com/trips/dest/australia-and-south-pacific/cntry/fiji/"

Now I tried this:

if (url.indexOf('australia-and-south-pacific') > 0) {return 'foo';}

Which returned:

SyntaxError: Illegal return statement

I expected the console to return 'foo'.

Why is this happening? I noticed that after receiving this error, give it afew seconds and a new error appears:

Uncaught TypeError: Cannot read property 'hide' of undefined 

If that helps to diagnose?

3
  • 2
    Because you are not in a function, so there is nothing to return to, maybe you wanted: console.log('foo') Commented Sep 5, 2014 at 14:49
  • 1
    You can't put a return statement anywhere but a function. Commented Sep 5, 2014 at 14:49
  • Ah OK, thank you I will update. Sorry this must have been super simple Commented Sep 5, 2014 at 14:49

1 Answer 1

8

According to the EMCAScript language specification,

An ECMAScript program is considered syntactically incorrect if it contains a return statement that is not within a FunctionBody.

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

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.