Types of Function
- Function Declaration
- Function Expression
- Function Arrow
*1.Function Declaration *
A Function Declaration is a function that is declared using the function keyword and has a function name. It can be called before or after its declaration because JavaScript hoists function declarations
Hoiting:Hoisting is JavaScript's behavior of moving declarations to the top of their scope before execution. Function declarations are fully hoisted
Syntax
function functionName(parameters) {
}
Example
function sampleFun(){
console.log("Hello World");
}
sampleFun()
2.Function Expression
A Function Expression is a function that is stored in a variable
Syntax
let variableName = function() {
}
Example
var sampleFun=function(){
console.log("Hello World");
}
sampleFun()
Output:Hello World
3.Arrow Function
Arrow Function is a shorter way to write a function
Introduced in ES6 (ECMAScript 2015)
uses the => (arrow) syntax
Syntax
let functionName = (parameters) => {
}
Example
var sampleFun=() =>{
console.log("Hello World");
}
sampleFun()
Output:Hello World