Functions
The Function object provides methods for functions. In JavaScript, every function is actually a Function object.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 1 | 1 | 18 | 1 | |
| The [Symbol.hasInstance]() method of Function instances specifies the default procedure for determining if a constructor function recognizes an object as one of the constructor's instances. It is called by the instanceof operator. | 50 | 15 | 50 | 10 | 50 | 10 |
| The Function() constructor creates Function objects. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as Global_Objects/eval. However, unlike eval (which may have access to the local scope), the Function constructor creates functions which execute in the… | 1 | 12 | 1 | 1 | 18 | 1 |
| The apply() method of Function instances calls this function with a given this value, and arguments provided as an array (or an array-like object). | 1 | 12 | 1 | 1 | 18 | 1 |
Function.apply.generic arrays as arguments ES 5.1: generic array-like object as `arguments` | 17 | 12 | 4 | 6 | 18 | 6 |
| The bind() method of Function instances creates a new function that, when called, calls this function with its this keyword set to the provided value, and a given sequence of arguments preceding any provided when the new function is called. | 7 | 12 | 4 | 5.1 | 18 | 6 |
| The call() method of Function instances calls this function with a given this value and arguments provided individually. | 1 | 12 | 1 | 1 | 18 | 1 |
| The length data property of a Function instance indicates the number of parameters expected by the function. | 1 | 12 | 1 | 1 | 18 | 1 |
Function.length.configurable true Configurable: true | 43 | 12 | 37 | 10 | 43 | 10 |
| The name data property of a Function instance indicates the function's name as specified when it was created, or it may be either anonymous or '' (an empty string) for functions created anonymously. | 15 | 14 | 1 | 6 | 18 | 6 |
Function.name.configurable true Configurable: true | 43 | 14 | 38 | 10 | 43 | 10 |
Function.name.inferred names Inferred names on anonymous functions | 51 | 79 | 53 | 10 | 51 | 10 |
| The toString() method of Function instances returns a string representing the source code of this function. | 1 | 12 | 1 | 1 | 18 | 1 |
Function.toString.toString revision Implements `Function.prototype.toString` revision | 66 | 79 | 54 | 17 | 66 | 17 |
| Other | ||||||
| Generally speaking, a function is a "subprogram" that can be called by code external (or internal, in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function as parameters, and the function will return a value. | 1 | 12 | 1 | 1 | 18 | 1 |
| arguments is an array-like object accessible inside functions that contains the values of the arguments passed to that function. | 1 | 12 | 1 | 1 | 18 | 1 |
| The [Symbol.iterator]() method of Functions/arguments objects implements the iterable protocol and allows arguments objects to be consumed by most syntaxes expecting iterables, such as the spread syntax and Statements/for...of loops. It returns an array iterator object that yields the value of each index in the arguments object. | 52 | 12 | 46 | 9 | 52 | 9 |
| The arguments.length data property contains the number of arguments passed to the function. | 1 | 12 | 1 | 1 | 18 | 1 |
| An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage: | 45 | 12 | 22 | 10 | 45 | 10 |
javascript.functions.arrow_functions.trailing_comma Trailing comma in parameters | 58 | 12 | 52 | 10 | 58 | 10 |
javascript.functions.block_level_functions Block-level functions | 49 | 12 | 46 | 10 | 49 | 10 |
| Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. | 49 | 14 | 15 | 10 | 49 | 10 |
javascript.functions.default_parameters.destructured_parameter_with_default_value_assignment Destructured parameter with default value assignment | 49 | 14 | 41 | 10 | 49 | 10 |
javascript.functions.default_parameters.parameters_without_defaults_after_default_parameters Parameters without defaults after default parameters | 49 | 14 | 26 | 10 | 49 | 10 |
| The get syntax binds an object property to a function that will be called when that property is looked up. It can also be used in classes. | 1 | 12 | 1.5 | 3 | 18 | 1 |
javascript.functions.get.computed_property_names Computed property names | 46 | 12 | 34 | 9.1 | 46 | 9.3 |
| Method definition is a shorter syntax for defining a function property in an object initializer. It can also be used in classes. | 39 | 12 | 34 | 9 | 39 | 9 |
javascript.functions.method_definitions.async_methods Async methods | 55 | 15 | 52 | 10.1 | 55 | 10.3 |
javascript.functions.method_definitions.generator_methods_not_constructable Generator methods are not constructable (ES2016) | 42 | 13 | 43 | 9.1 | 42 | 9.3 |
| The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadic functions in JavaScript. | 47 | 12 | 15 | 10 | 47 | 10 |
javascript.functions.rest_parameters.destructuring Destructuring rest parameters | 49 | 79 | 52 | 10 | 49 | 10 |
| The set syntax binds an object property to a function to be called when there is an attempt to set that property. It can also be used in classes. | 1 | 12 | 1.5 | 3 | 18 | 1 |
javascript.functions.set.computed_property_names Computed property names | 46 | 12 | 34 | 9.1 | 46 | 9.3 |
| Grammar | ||||||
trailing commas in functions Trailing comma in function parameters | 58 | 14 | 52 | 10 | 58 | 10 |
| Other | ||||||
| The function keyword can be used to define a function inside an expression. | 1 | 12 | 1 | 1 | 18 | 1 |
| Operator | ||||||
trailing comma Trailing comma in parameters | 58 | 14 | 52 | 10 | 58 | 10 |
| Other | ||||||
| The function declaration creates a binding of a new function to a given name. | 1 | 12 | 1 | 1 | 18 | 1 |
| Statement | ||||||
trailing comma in parameters Trailing comma in parameters | 58 | 14 | 52 | 10 | 58 | 10 |
- This browser only partially implements this feature
- This feature was removed in a later browser version (79)
- Names for functions defined in a dictionary are properly assigned; however, anonymous functions defined on a var/let variable assignment have blank names.
- The initial implementation of arrow functions in Firefox made them automatically strict. This has been changed as of Firefox 24. The use of `'use strict';` is now required.
- Before Firefox 39, a line terminator (`\n`) was incorrectly allowed after arrow function arguments. This has been fixed to conform to the ES2015 specification and code like `() \n => {}` will now throw a `SyntaxError` in this and later versions.
Syntax
// Default arguments
function greet(name = 'Guest') {
return `Hello, ${name}!`;
}
// Rest arguments
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0);
}
sum(1, 2, 3, 4); // 10 Live demo
defaultargument. Usage
Argument that specified case to defaultvalue that apply variantchild simulated.
aggregate with restargument.
not-specific. number. Argument and processingrestargument. behavior inspect it..
callbackfunction. asyncrun
Function argument and, specific. Processing that Doneafter to Runmechanism simulated.
Use cases
-
Using Functions
The Function object provides methods for functions.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.