Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

SyntaxError: return not in function

我們的志工尚未將此文章翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!

Message

SyntaxError: return not in function
SyntaxError: yield not in function

Error type

SyntaxError.

What went wrong?

A return or yield statement is called outside of a function. Maybe there are missing curly brackets somewhere? The return and yield statements must be in a function, because they end (or pause and resume) function execution and specify a value to be returned to the function caller.

Examples

var cheer = function(score) {
  if (score === 147)
    return "Maximum!";
  };
  if (score > 100) {
    return "Century!";
  }
}

// SyntaxError: return not in function

The curly brackets look correct at a first glance, but this code snippet is missing a { after the first if statement. Correct would be:

var cheer = function(score) {
  if (score === 147) {
    return "Maximum!";
  }
  if (score > 100) {
    return "Century!";
  }
};

See also

文件標籤與貢獻者

 此頁面的貢獻者: fscholz, lewisje
 最近更新: fscholz,