Function Declaration (vs) Function Expression
Function Declaration:
|
Function Expression: |
- A function created with function keyword and a function name.
- Function Declarations are "Hoisted".
|
- A function created annonymously, without name
- Function Expression are "Not-hoisted".
-
It can be assigned to a variable and we can call the function using that variable.
(or)
It can be used as a callback function
(or)
It can be used as a IIFE function
|
let param1 = "Name";
let param2 = "Name2";
// FUNCTION DECLARATION -Start ######################
dd(param1)
function dd(p1) {
console.log('This is normal function declaration', param1);
return p1
}
// FUNCTION DECLARATION -End ######################
// FUNCTION EXPRESSION -Start #####################
//Normal function expression
let ee = function (p1) {
console.log('This is normal function expression', param1);
return p1
}
ee(param1)
// Arrow Function Expression
let ee2 = (p1) => {
console.log('This is normal function expression', param1);
return p1
}
ee2(param1)
// IIFE (Immediately Invoked Function Expression)
(function () {
console.log('This is normal function expression', param1);
//...
})()
// FUNCTION EXPRESSION -End ######################