- 1. Function expression
- 2. Function declaration
A function expression is a function that is assigned to a variable. The function is created and then immediately assigned to a variable. For example:
In this example, the function is assigned to the variable "add", and can be called by calling the variable and passing in the appropriate arguments.
A function declaration, on the other hand, is a function that is declared directly as a statement. The function is created and given a name, and can be called by using the function's name. For example:
In this example, the function is declared with the name "add" and can be called by using this name.
Both function expressions and function declarations are hoisted, which means that they can be called before they are defined in the code.
There is a subtle difference between function expressions and function declarations. Function expressions are not hoisted with their assignment, only the variable is hoisted. Function declarations are hoisted with the function definition.
Function expressions are useful when you need to create a function and immediately assign it to a variable, while function declarations are useful when you need to create a function and give it a name.
That's it. Hope you find this useful. Happy Coding!