-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
84 lines (62 loc) · 2.27 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: "Warning: unreachable code after return statement"
slug: Web/JavaScript/Reference/Errors/Stmt_after_return
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript warning "unreachable code after return statement" occurs when using an
expression after a {{jsxref("Statements/return", "return")}} statement, or when using a
semicolon-less return statement but including an expression directly after.
## Message
```plain
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 a {{jsxref("Statements/return", "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:
- {{jsxref("Statements/throw", "throw")}}
- {{jsxref("Statements/break", "break")}}
- {{jsxref("Statements/var", "var")}}
- {{jsxref("Statements/function", "function")}}
## Examples
### Invalid cases
```js-nolint example-bad
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-nolint example-good
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
- [Automatic semicolon insertion](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion)