MDN wants to talk to developers like you: https://qsurvey.mozilla.com/s3/a3e7b5301fea

Warning: unreachable code after return statement

Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.

Сообщение

Warning: unreachable code after return statement (Firefox)

Тип ошибки

Warning

Что пошло не так?

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

  • When using an expression after a return statement, or
  • when 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:

Примеры

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
}

Смотри так же

Метки документа и участники

 Внесли вклад в эту страницу: naudachu, s1lver
 Обновлялась последний раз: naudachu,