The JavaScript exception "malformed formal parameter" occurs when the argument list of a Function()
constructor call is invalid somehow.
The JavaScript exception "malformed formal parameter" occurs when the argument list of a Function()
constructor call is invalid somehow.
SyntaxError: Expected {x} (Edge) SyntaxError: malformed formal parameter (Firefox)
There is a Function()
constructor with at least two arguments passed in the code. The last argument is the source code for the new function you're creating. All the rest make up your new function's argument list.
The argument list is invalid somehow. Perhaps you accidentally picked a keyword like if
or var
as an argument name, or perhaps there's some stray punctuation in your argument list. Or maybe you accidentally passed an invalid value, like a number or object.
Admittedly the wording in the error message is slightly strange. "Formal parameter" is a fancy way of saying "function argument". And we use the word "malformed" because all Firefox engineers are huge fans of 19th-century Gothic horror novels.
const f = Function("x y", "return x + y;"); // SyntaxError (missing a comma) const g = Function(37, "alert('OK')"); // SyntaxError (numbers can't be argument names)
const f = Function("x, y", "return x + y;"); // correctly punctuated // if you can, avoid using Function - this is much faster const g = function (x) { return x; };
Function()
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_formal_parameter