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.

Revision 1066524 of SyntaxError: return not in function

  • Revision slug: Web/JavaScript/Reference/Errors/Bad_return_or_yield
  • Revision title: SyntaxError: return not in function
  • Revision id: 1066524
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment new page

Revision Content

{{jsSidebar("Errors")}}

Message

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

Error type

{{jsxref("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 snipped 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

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
SyntaxError: return not in function
SyntaxError: yield not in function
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("SyntaxError")}}.</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>A <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/return">return</a></code> or <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/yield">yield</a></code> statement is called outside of a <a href="/en-US/docs/Web/JavaScript/Guide/Functions">function</a>. Maybe there are missing curly brackets somewhere? The <code>return</code> and <code>yield</code> 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.</p>

<h2 id="Examples">Examples</h2>

<pre class="brush: js example-bad">
var cheer = function(score) {
  if (score === 147)
    return "Maximum!";
  };
  if (score &gt; 100) {
    return "Century!";
  }
}

// SyntaxError: return not in function</pre>

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

<pre class="brush: js example-good">
var cheer = function(score) {
  if (score === 147) {
    return "Maximum!";
  }
  if (score &gt; 100) {
    return "Century!";
  }
};</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/return">return</a></code></li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/yield">yield</a></code></li>
</ul>
Revert to this revision