function
function – Declares a function with the specified parameters.
Description
function ([mixed param, …])
Parameters
Name | Description | Type | Default | Optional |
---|---|---|---|---|
param, … | Variables to pass into the function. | mixed | Yes |
Changelog
Version | Description |
---|---|
ES 6 | Ability to set default values in function declaration. |
ES 6 | Agregating parameter overflow. |
Examples
Example #1 – function example
Basic syntax for creating a function.
We can also declare a function by assigning a variable to an unnamed function.
Example #2 – function example
Create a self executing function.
Passing variables into a self executing function.
Example #3 – function example
In javascript you do not need to pass any of the functions parameters. So if you wish to create parameters with default values, you can use something like the example below where the b parameter will be set to a default value if it is not passed when calling the function.
A shorthand version.
Example #4 – function example
Accessing arguments passed to the function.
Example #5 – function example
Setting default values. Available from ES 6.
Example #6 – function example
Agregating parameter overflow. Available from ES 6.
Example #7 – function example
Spreading variable in function call. Available from ES 6.
Example #8 – function example
Spreading variable in function call. Available from ES 6.