JavaScript SyntaxError - Malformed formal parameter
This JavaScript exception malformed formal parameter occurs if the argument list of a Function() constructor call is not valid.
Message:
SyntaxError: Expected {x} (Edge)
SyntaxError: malformed formal parameter (Firefox)
Error Type:
SyntaxError
Cause of Error: The argument list passed to the function is not valid. The if or var can not be picked as an argument name, or there's some stray punctuation in the argument list. The invalid value passed can cause a problem, like a number or an object.
Case 1: Using Invalid Characters in Parameter Names
Error Cause:
Using invalid characters in parameter names, such as symbols or spaces, will cause this error.
Example:
function exampleFunction(x, y@) { // Incorrect usage
return x + y;
}
Output:
SyntaxError: Malformed formal parameter
Resolution of Error:
Use valid parameter names that consist of letters, digits, underscores, or dollar signs.
function exampleFunction(x, y) { // Correct usage
return x + y;
}
console.log(exampleFunction(2, 3)); // Outputs: 5
Output
5
Case 2: Incorrect Punctuation in Parameter List
Error Cause:
Using incorrect punctuation, such as an extra comma, in the parameter list.
Example:
function exampleFunction(x,, y) { // Incorrect usage
return x + y;
}
Output:
SyntaxError: Malformed formal parameter
Resolution of Error:
Ensure that the parameter list is correctly punctuated.
function exampleFunction(x, y) { // Correct usage
return x + y;
}
console.log(exampleFunction(2, 3)); // Outputs: 5
Output
5
Case 3: Malformed Destructuring in Parameter List
Error Cause:
Malformed destructuring assignments in the parameter list can also cause this error.
Example:
function exampleFunction({x, y,,}) { // Incorrect usage
return x + y;
}
Output:
SyntaxError: Malformed formal parameter
Resolution of Error:
Ensure that destructuring assignments are correctly formatted.
function exampleFunction({x, y}) { // Correct usage
return x + y;
}
console.log(exampleFunction({x: 2, y: 3})); // Outputs: 5
Output
5