JavaScript Function Object
Every JavaScript function is a Function object. It can be called, passed around like any other value, and has methods such as call(), apply() and bind() to control what this refers to.
Description
In JavaScript, functions are first-class objects: they can be stored in variables, passed to other functions and returned from them, and they can have properties. Every function is an instance of Function, whether it was written as a function declaration, a function expression, an arrow function or a class method.
Function.prototype provides call(), apply() and bind(), which run a function with a chosen this value, and the name and length properties. Creating functions with new Function('a', 'return a') is possible but rarely a good idea: like eval(), it compiles a string at run time and is blocked by many Content Security Policies.
Functions are also closures: they remember the variables in scope where they were created, even after that code has finished running. Regular functions have their own this, set by how they are called, while arrow functions use the this of the surrounding code. A function's name and length properties report its name and number of declared parameters, and functions can have any other properties you add.
Creating
function name(params) { ... }
(params) => { ... }Instance properties
| Name | Description |
|---|---|
Function.prototype.length | The length property of a function is the number of parameters it declares, not counting rest parameters or those with default values. |
Function.prototype.name | The name property of a function holds its name, which is shown in stack traces and debugging tools. |
Instance methods
| Name | Description |
|---|---|
Function.prototype.apply() | The apply() method calls a function with a given this value and its arguments supplied as an array. |
Function.prototype.bind() | The bind() method creates a new function with this permanently set to a given value, and optionally some arguments filled in. |
Function.prototype.call() | The call() method calls a function with a given this value and arguments listed one by one. |
Example
Browser Support
The following information will show you the current browser support for the Function object. Hover over a browser icon to see the version that first introduced support for this object.
Desktop





Tablets & Mobile





