A function declaration defines a function that will be executed when it is invoked.
function shot() {
// body
}
A function expression is similar to a function declaration, with the difference that it can be stored in a variable. As soon as the function is defined with an expression, it is invoked.
const shot = function() {
// body
}
1. Hoisting:
Defining a function using function declaration:
Defining a function using function expression:
2. Callback:
Callback using function declaration:
Callback using function expression:
3. IIFE:
These are some basic differences between function declaration and function expression. The usage of them depends on the use case. If the function is required globally, use function declaration. If the scope of the function needs to be limited, use function expression.