- Identify the name of the function.
- List the parameters for the function, with each parameter delimited by a comma and enclosed in parentheses.
- Named function
- Anonymous function
- Immediately invoked function expression
A named function is the type of function that’s constructed in the code and then called by referencing its name and giving it parameters. Named functions come in handy when you need to call a function to run it multiple times or feed it different values.
Here is an example of what a named function looks like:
The functions known as anonymous functions do not have their own names. To execute an operation or compute a value, they need to be connected to something, such as a variable or an event.
Below is the same function as in the last example, but this time it’s anonymous:
Immediately Invoked Function Expression
The expression of the invoked function is executed as soon as the browser comes across it. The benefit of using this function is that it provides an output that is produced directly and runs immediately wherever it is contained in the code. This means the function is not affected by code that comes later in the script, which can be helpful.
Using an invoked function expression is an ideal option when you need to quickly populate a variable or argument in a larger function or a property in an object. These expressions are frequently attached to event listeners to produce immediate output.