Types of function

javascript dev.to

Types of Function

  1. Function Declaration
  2. Function Expression
  3. 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) {
}
Enter fullscreen mode Exit fullscreen mode

Example

function sampleFun(){
    console.log("Hello World");
}
sampleFun()
Enter fullscreen mode Exit fullscreen mode

2.Function Expression
A Function Expression is a function that is stored in a variable

Syntax

let variableName = function() {
}
Enter fullscreen mode Exit fullscreen mode

Example

var sampleFun=function(){
    console.log("Hello World");
}
sampleFun()
Enter fullscreen mode Exit fullscreen mode

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) => {    
}
Enter fullscreen mode Exit fullscreen mode

Example

var sampleFun=() =>{
    console.log("Hello World");
}
sampleFun()
Enter fullscreen mode Exit fullscreen mode

Output:Hello World

Source: dev.to

arrow_back Back to Tutorials