Function constructor
The Function constructor creates a new function from strings at runtime: you pass the parameter names and the body as text, and JavaScript compiles them into a working function. It also explains a fact about the language itself: every function you write is an object created from Function. You will rarely need it in everyday code, but understanding it clarifies how functions work as objects and why it is usually avoided.
The syntax
All arguments are strings. The last one is the function body; any before it are parameter names.
const add = new Function('a', 'b', 'return a + b;');
console.log(add(2, 3)); // 5
// Parameters can also be in one comma-separated string
const greet = new Function('name, greeting = "Hello"',
'return `${greeting}, ${name}!`;');
console.log(greet('Neha')); // Hello, Neha!
// Calling without new works the same way
const square = Function('x', 'return x * x;');
console.log(add instanceof Function); // true
console.log((function () {}) instanceof Function); // true
console.log(typeof Function.prototype.call); // 'function'
Because every function is an instance of Function, they all inherit methods from Function.prototype, including call, apply and bind. This connects to the previous lesson on functions as objects: functions can have properties, can be passed around, and have a prototype chain just like other objects.
Scope works differently here
This is the most important behaviour to understand. A function created with new Function does not close over the variables where it was created. It can only see its own parameters and global variables.
function makeMultiplier() {
const factor = 3;
const viaLiteral = (n) => n * factor;
const viaConstructor = new Function('n', 'return n * factor;');
console.log(viaLiteral(2)); // 6
console.log(viaConstructor(2)); // ReferenceError: factor is not defined
}
A normal function expression remembers the scope it was written in, which is the closure behaviour covered later in this course. The Function constructor always creates its function in the global scope, so factor is invisible to it. Beginners who expect it to behave like a regular function are often confused by this ReferenceError. If you need data inside the generated function, pass it in as a parameter.
Why it is usually avoided
- Security: like
eval, it executes code from a string. If any part of that string comes from user input, an attacker can run their own code on your page. - Content Security Policy: many sites set a CSP header that blocks string-to-code evaluation, and
new Functionthen throws an error unless the policy explicitly allowsunsafe-eval. - Tooling: code inside strings gets no syntax highlighting, no linting and no help from minifiers, and errors only appear when it runs.
- Readability: other developers cannot see the logic at a glance.
Where it does show up
Template engines and some library build tools generate functions from strings to speed up repeated work, and you may meet it in older code that loaded formulas from configuration. For normal programs, function declarations, function expressions and arrow functions cover every case more safely.
Note that the Function constructor is different from a constructor function. A constructor function is one you write yourself and call with new to build objects. Function is the built-in that builds functions.
What to remember
new Function(params..., body)compiles strings into a function.- It sees only its parameters and globals, never the surrounding local scope.
- Every function inherits from
Function.prototype, which is wherecall,applyandbindcome from. - Prefer normal function syntax; reserve this for rare code-generation tasks.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.