SyntaxError: missing formal parameter
The JavaScript exception "missing formal parameter" occurs when your functiondeclaration is missing valid parameters.
Message
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
What went wrong?
"Formal parameter" is a fancy way of saying "function parameter". Your functiondeclaration is missing valid parameters. In the declaration of a function, theparameters must beidentifiers, not anyvalue like numbers, strings, or objects. Declaring functions and calling functions aretwo separate steps. Declarations require identifier as parameters, and only when calling(invoking) the function, you provide the values the function should use.
InJavaScript, identifiers can containonly alphanumeric characters (or "$" or "_"), and may not start with a digit. Anidentifier differs from astring in that a string is data, while anidentifier is part of the code.
Examples
Provide proper function parameters
Function parameters must be identifiers when setting up a function. All these functiondeclarations fail, as they are providing values for their parameters:
function square(3) { return number * number;}// SyntaxError: missing formal parameterfunction greet("Howdy") { return greeting;}// SyntaxError: missing formal parameterfunction log({ obj: "value"}) { console.log(arg)}// SyntaxError: missing formal parameter
You will need to use identifiers in function declarations:
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:
square(2); // 4greet("Howdy"); // "Howdy"log({ obj: "value" }); // { obj: "value" }