最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught SyntaxError: Unexpected token return - still no answer? - Stack Overflow

programmeradmin4浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

Because 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.

发布评论

评论列表(0)

  1. 暂无评论