Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Unexpected token return - still no answer?

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?

like image 802
Dmitri Zaitsev Avatar asked Dec 22 '13 02:12

Dmitri Zaitsev


People also ask

How do I get rid of unexpected token error?

Luckily, the SyntaxError: Unexpected token error is relatively easy to fix. In most cases, the error can be resolved by checking the code for accuracy and correcting any mistakes. There are also a number of tools and resources available to help you debug and fix JavaScript code.

How do I fix uncaught SyntaxError unexpected identifier?

To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.

What is uncaught SyntaxError unexpected token '<'?

The error Uncaught SyntaxError: Unexpected token < is most commonly caused by your site's code referring to an asset that is no longer available. Most commonly, this is due to a filename change in assets generated during your build.


1 Answers

Because return is not an expression, but it expects an expression:

function f() {
  return false || true;
}
like image 145
elclanrs Avatar answered Nov 01 '22 06:11

elclanrs