Uncaught SyntaxError: Unexpected token return - no response yet?

Thus, there are dozens of questions with this name, however, all the answers that I could find seem to be mentioned by some hacks that work in some specific cases, but are not useful in others. Many of these are related to jQuery or Ajax, but the problem is pure JavaScript, which occurs at a very basic level:

function f() { false || (return true); } 

Declaring this function (no execution) throws

Uncaught SyntaxError: Unexpected token return

in Chrome and

SyntaxError: Return statements are only valid inside functions

in Safari. However, this function does not perform:

 function f() { false || (a=true); return true; } 

Can anyone explain this strange behavior?

+6
source share
2 answers

Because return not an expression, but expects an expression:

 function f() { return false || true; } 
+3
source

You use the return in the expression as an expression that is not possible because the JavaScript engine cannot evaluate it. That is why it throws an error.

+3
source

All Articles