Different Types of Function
- Function Declaration
- Function Expression
- Anonymous Function Expression
- Arrow function
Function Declaration
When a function is starting with keyword function that kind of function is known as function declration.
ex-
function add(a,b){
return a+b
}
add(1,2)
3
Function Expression
When a function is starting with expression that kind of function is known as function expression.
- Expression is anything that results into value
- The function that stored in a variable known as function expression
- In function expression you can avoid the name of the function because in function expression name has no Use
- function name is optional
- In function expression you will always call the function with the name of the variable not with name of the function.
ex-
const addnumber = function add (num1,num2){
return num1 + num2
}
add(10,12)
22
Anonymous Function Expression
When there is no name to function that kind of function is known as Anonymous function
ex-
const addnumber = function (num1,num2){
return num1 + num2
}
addNumber(10,10)
20
Arrow Function
Arrow function is a shorter way of writting @Anonymous function
- The way you make it short is by removing keyword function from it
- You add special sambol ( => eqaul to ,greater than) known as aerrow
- Remove function and replace with ( => )aerrow
- You can never name a Arrow function
- Arrow function always be anonymous
ex-
const addnumber = (num1,num2) =>{ return num1+num2 } addNumber(2,3) 5