Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Warning: unreachable code after return statement

The JavaScript warning "unreachable code after return statement" occurs when using anexpression after areturn statement, or when using asemicolon-less return statement but including an expression directly after.

Message

Warning: unreachable code after return statement (Firefox)

Error type

Warning

What went wrong?

Unreachable code after a return statement might occur in these situations:

  • When using an expression after areturnstatement, or
  • when using a semicolon-less return statement but including an expression directlyafter.

When an expression exists after a validreturn statement, a warning isgiven to indicate that the code after thereturn statement is unreachable,meaning it can never be run.

Why should I have semicolons afterreturn statements? In the case ofsemicolon-lessreturn statements, it can be unclear whether the developerintended to return the statement on the following line, or to stop execution and return.The warning indicates that there is ambiguity in the way thereturnstatement is written.

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

Examples

Invalid cases

js
function f() {  let x = 3;  x += 4;  return x;   // return exits the function immediately  x -= 3;     // so this line will never run; it is unreachable}function g() {  return     // this is treated like `return;`    3 + 4;   // so the function returns, and this line is never reached}

Valid cases

js
function f() {  let x = 3;  x += 4;  x -= 3;  return x; // OK: return after all other statements}function g() {  return 3 + 4 // OK: semicolon-less return with expression on the same line}

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp