1.Named Function:
- A function is created using the
functionkeyword with a name.
Syntax:
function functionName() {
// code
}
Example:
// Named Function
function addition(a,b,c)
{
console.log(a+b+c);
}
addition(40,20,40);
Output:
100
2.Function Expression
- A function is stored inside a variable.
Syntax:
let variableName = function() {
// code
};
Example:
// Function expression
let greet=function(){
console.log("Hello");
};
greet();
Output:
Hello
3.Arrow Function:
- A shorter way to write functions.
Syntax:
const functionName = () => {
// code
};
Example:
const multiply=(a,b)=>{
console.log(a*b);
}
multiply(10,20);
Output:
200
Arrow Function With One Parameter:
Example:
const square = num => num * num;
console.log(square(5));
Output:
25
Parentheses are optional when there is only one parameter.
4.Immediately Invoked Function Expression (IIFE):
- A function that is created and executed immediately.
Syntax:
(function() {
//code
})();
Example:
(function() {
console.log("Hello");
})();
Output:
Hello
Top comments (2)
Function expression and storing a function in a variable are two different things
Yes, they're related concepts, but they aren't the same thing.