So there are dozens of questions with this title, however, all answers I could find seem to mention some hacks working in some specific cases but not being helpful in others. Many are concerned with jQuery or Ajax, yet the problem is pure JavaScript arising at very basic level:
function f() {
false || (return true);
}
This function declaration (without execution) throws
Uncaught SyntaxError: Unexpected token return
in Chrome and
SyntaxError: Return statements are only valid inside functions
in Safari. However this function doesn't:
function f() {
false || (a=true);
return true;
}
Anybody can explain this strange behaviour?
So there are dozens of questions with this title, however, all answers I could find seem to mention some hacks working in some specific cases but not being helpful in others. Many are concerned with jQuery or Ajax, yet the problem is pure JavaScript arising at very basic level:
function f() {
false || (return true);
}
This function declaration (without execution) throws
Uncaught SyntaxError: Unexpected token return
in Chrome and
SyntaxError: Return statements are only valid inside functions
in Safari. However this function doesn't:
function f() {
false || (a=true);
return true;
}
Anybody can explain this strange behaviour?
Share Improve this question asked Dec 22, 2013 at 2:38 Dmitri ZaitsevDmitri Zaitsev 14.1k12 gold badges78 silver badges114 bronze badges2 Answers
Reset to default 5Because return
is not an expression, but it expects an expression:
function f() {
return false || true;
}
You are using return
statement in an expression, as an expression, which is not possible as JavaScript engine cannot evaluate it. Thats why it is throwing the error.