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') Sep 5, 2014 at 14:49
  • 1
    You can't put a return statement anywhere but a function.
    – scrblnrd3
    Sep 5, 2014 at 14:49
  • Ah OK, thank you I will update. Sorry this must have been super simple
    – Doug Fir
    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.