-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
59 lines (43 loc) · 1.55 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
---
title: "SyntaxError: return not in function"
slug: Web/JavaScript/Reference/Errors/Bad_return
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript exception "return not in function" occurs when a [`return`](/en-US/docs/Web/JavaScript/Reference/Statements/return) statement is called outside of a [function](/en-US/docs/Web/JavaScript/Guide/Functions).
## Message
```plain
SyntaxError: Illegal return statement (V8-based)
SyntaxError: return not in function (Firefox)
SyntaxError: Return statements are only valid inside functions. (Safari)
```
## Error type
{{jsxref("SyntaxError")}}.
## What went wrong?
A [`return`](/en-US/docs/Web/JavaScript/Reference/Statements/return) statement is called outside of a [function](/en-US/docs/Web/JavaScript/Guide/Functions). Maybe there are missing curly braces somewhere? The `return` statement must be in a function, because it ends function execution and specifies a value to be returned to the function caller.
## Examples
### Missing curly braces
```js-nolint example-bad
function cheer(score) {
if (score === 147)
return "Maximum!";
}
if (score > 100) {
return "Century!";
}
}
// SyntaxError: return not in function
```
The curly braces look correct at a first glance, but this code snippet is missing a `{` after the first `if` statement. Correct would be:
```js example-good
function cheer(score) {
if (score === 147) {
return "Maximum!";
}
if (score > 100) {
return "Century!";
}
}
```
## See also
- [`return`](/en-US/docs/Web/JavaScript/Reference/Statements/return)