Function
The Function global object is the constructor behind ordinary JavaScript functions, and its prototype holds the methods that all functions share, such as .call(), .apply(), and .bind(). Functions written with the function keyword, arrow functions, and class constructors all inherit from Function.prototype:
> function greet(name) { return `Hello, ${name}!`; }
undefined
> greet instanceof Function
true
> Object.getPrototypeOf(greet) === Function.prototype
true
Function is an ECMAScript built-in. Calling it directly builds a new function from strings at runtime, which is rare in application code. Its everyday role is the shared prototype that gives every function its methods and properties:
Syntax
The Function constructor takes zero or more parameter names followed by the function body, all as strings:
new Function([<param1>, ..., <paramN>,] <body>)
| Parameter | Description |
|---|---|
param1, …, paramN |
Optional strings naming the parameters. Each string can also hold several comma-separated names, defaults, or a rest parameter |
body |
An optional string with the statements of the function body. It defaults to an empty body |
The constructor returns a new function named anonymous. The new keyword is optional, because Function() without it behaves the same. The body is parsed in the global scope and runs in sloppy mode unless it starts with a "use strict" directive.
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.call() |
Instance method | Calls the function with a given this value and individual arguments |
.apply() |
Instance method | Calls the function with a given this value and an array-like object of arguments |
.bind() |
Instance method | Returns a new function with a fixed this value and optional leading arguments |
.toString() |
Instance method | Returns the source text of the function, or a [native code] placeholder for built-ins |
.length |
Instance property | Holds the number of parameters before the first default or rest parameter |
.name |
Instance property | Holds the function’s name, inferred from the binding when the function is anonymous |
.prototype |
Instance property | Holds the object that new gives instances as their prototype. Arrow functions, methods, and async functions don’t have one |
[Symbol.hasInstance]() |
Instance method | Decides whether an object is an instance of the function, which instanceof uses |
Examples
A formatting helper that reads its data from this can work on any object. The .call() method passes arguments one by one, while .apply() takes them as an array:
> function describe(unit) {
… return `${this.label}: ${this.value} ${unit}`;
… }
undefined
> const reading = { label: "Weight", value: 72 };
undefined
> describe.call(reading, "kg")
'Weight: 72 kg'
> describe.apply(reading, ["lb"])
'Weight: 72 lb'
A method passed as a callback loses its this binding, so the method can’t reach its own object. The .bind() method fixes this before the callback leaves the object:
> const cart = {
… items: [],
… add(item) {
… this.items.push(item);
… return this.items.length;
… },
… };
undefined
> ["apple", "pear"].map(cart.add)
Uncaught TypeError: Cannot read properties of undefined (reading 'push')
at add (REPL7:4:16)
at Array.map (<anonymous>)
> ["apple", "pear"].map(cart.add.bind(cart))
[ 1, 2 ]
A test runner can read .length to tell whether a test function expects a done callback or works without one:
> const expectsCallback = (test) => test.length > 0;
undefined
> expectsCallback((done) => setTimeout(done, 10))
true
> expectsCallback(async () => {})
false
A pricing tool that stores formulas as text in a config file can compile each formula into a callable function with the constructor:
> const formula = "price * quantity";
undefined
> const total = new Function("price", "quantity", `return ${formula};`);
undefined
> total(2.5, 4)
10
> total.name
'anonymous'
Common Gotchas
A function built by the constructor doesn’t close over the scope where it was created. It sees only global variables, so a local variable in the surrounding function isn’t defined inside it:
> function makeScaler() {
… const factor = 3;
… return new Function("n", "return n * factor;");
… }
undefined
> makeScaler()(2)
Uncaught ReferenceError: factor is not defined
at eval (eval at makeScaler (REPL4:3:10), <anonymous>:3:12)
The constructor evaluates code from strings, so it carries the same risks as eval(). Browsers block it on pages with a Content Security Policy that restricts scripts, unless the policy allows 'trusted-types-eval' or 'unsafe-eval'.
Async functions and generator functions have their own constructors, which aren’t globals. Each one inherits from Function, and the only way to reach it is through an existing function:
> Object.getPrototypeOf(async () => {}).constructor.name
'AsyncFunction'
> Object.getPrototypeOf(function* () {}).constructor.name
'GeneratorFunction'
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 4, 2026