-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
90 lines (65 loc) · 2.37 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
85
86
87
88
89
90
---
title: "SyntaxError: missing formal parameter"
slug: Web/JavaScript/Reference/Errors/Missing_formal_parameter
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript exception "missing formal parameter" occurs when your function
declaration is missing valid parameters.
## Message
```plain
SyntaxError: missing formal parameter (Firefox)
SyntaxError: Unexpected number '3'. Expected a parameter pattern or a ')' in parameter list. (Safari)
SyntaxError: Unexpected string literal "x". Expected a parameter pattern or a ')' in parameter list. (Safari)
```
## Error type
{{jsxref("SyntaxError")}}
## What went wrong?
"Formal parameter" is a fancy way of saying "function parameter". Your function
declaration is missing valid parameters. In the declaration of a function, the
parameters must be [identifiers](/en-US/docs/Glossary/Identifier), not any
value like numbers, strings, or objects. Declaring functions and calling functions are
two separate steps. Declarations require identifier as parameters, and only when calling
(invoking) the function, you provide the values the function should use.
In [JavaScript](/en-US/docs/Glossary/JavaScript), identifiers can contain
only alphanumeric characters (or "$" or "\_"), and may not start with a digit. An
identifier differs from a **string** in that a string is data, while an
identifier is part of the code.
## Examples
### Provide proper function parameters
Function parameters must be identifiers when setting up a function. All these function
declarations fail, as they are providing values for their parameters:
```js-nolint example-bad
function square(3) {
return number * number;
}
// SyntaxError: missing formal parameter
function greet("Howdy") {
return greeting;
}
// SyntaxError: missing formal parameter
function log({ obj: "value"}) {
console.log(arg)
}
// SyntaxError: missing formal parameter
```
You will need to use identifiers in function declarations:
```js example-good
function square(number) {
return number * number;
}
function greet(greeting) {
return greeting;
}
function log(arg) {
console.log(arg);
}
```
You can then call these functions with the arguments you like:
```js
square(2); // 4
greet("Howdy"); // "Howdy"
log({ obj: "value" }); // { obj: "value" }
```
## See also
- [SyntaxError: redeclaration of formal parameter "x"](/en-US/docs/Web/JavaScript/Reference/Errors/Redeclared_parameter)