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 1028914 of Warning: unreachable code after return statement

  • Revision slug: Web/JavaScript/Reference/Errors/Stmt_after_return
  • Revision title: Warning: unreachable code after return statement
  • Revision id: 1028914
  • Created:
  • Creator: rolfedh
  • Is current revision? No
  • Comment Editorial review: Removed a false conditional.

Revision Content

{{jsSidebar("Errors")}}

Message

Warning: unreachable code after return statement (Firefox)

Error Type

Warning

What went wrong?

You were either:

  • using an expression after a {{jsxref("Statements/return", "return")}} statement
  • using a semicolon-less return statement but including an expression directly after

When an expression exists after a valid return statement, a warning is given to indicate that the code after the return statement is unreachable, meaning it can never be run.

Why should I have semicolons after return statements? In the case of semicolon-less return statements, it can be unclear whether the developer intended to return the statement on the following line, or to stop execution and return. The warning indicates that there is ambiguity in the way the return statement is written.

Warnings will not be shown for semicolon-less returns if these statements follow it:

  • throw
  • break
  • var
  • function

Examples

Invalid Cases

function f() {
  var x = 3;
  x += 4;
  return x;   // return exits the function immediately
  x -= 3;     // so this line will never run; it is unreachable
}

function f() {
  return     // this is treated like `return;`
    3 + 4;   // so the function returns, and this line is never reached
}

Valid cases

function f() {
  var x = 3;
  x += 4;
  x -= 3;
  return x;  // ok: return after all other statements
}

function f() {
  return 3 + 4  // ok: semicolon-less return with expression on the same line
}

See also

  • {{jsxref("Statements/return", "Automatic Semicolon Insertion", "#Automatic_Semicolon_Insertion")}}

Revision Source

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

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

<pre class="syntaxbox">
Warning: unreachable code after return statement (Firefox)
</pre>

<h2 id="Error_Type">Error Type</h2>

<p>Warning</p>

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

<p>You were either:</p>

<ul>
 <li>using an expression after a {{jsxref("Statements/return", "return")}} statement</li>
 <li>using a semicolon-less return statement but including an expression directly after</li>
</ul>

<p>When an expression exists after a valid return statement, a warning is given to indicate that the code after the return statement is unreachable, meaning it can never be run.</p>

<p>Why should I have semicolons after <code>return</code> statements? In the case of semicolon-less return statements, it can be unclear whether the developer intended to return the statement on the following line, or to stop execution and return. The warning&nbsp;indicates that there is ambiguity in the way the return statement is written.</p>

<p>Warnings will not be shown for semicolon-less returns if these statements follow it:</p>

<ul>
 <li>throw</li>
 <li>break</li>
 <li>var</li>
 <li>function</li>
</ul>

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

<h3 id="Invalid_cases">Invalid Cases</h3>

<pre class="brush: js example-bad">
function f() {
  var x = 3;
  x += 4;
  return x;   // return exits the function immediately
  x -= 3;     // so this line will never run; it is unreachable
}

function f() {
  return     // <span class="author-a-z71z8yd1z76zaz69zxz76zz65z614z74zz71z">this is treated like `return;`</span>
    3 + 4;   // so the function returns, and this line is never reached
}
</pre>

<h3 id="Valid_cases">Valid cases</h3>

<pre class="brush: js example-good">
function f() {
  var x = 3;
  x += 4;
  x -= 3;
  return x;  // ok: return after all other statements
}

function f() {
  return 3 + 4  // ok: semicolon-less return with expression on the same line
}
</pre>

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

<ul>
 <li>{{jsxref("Statements/return", "Automatic Semicolon Insertion", "#Automatic_Semicolon_Insertion")}}</li>
</ul>
Revert to this revision