DEV Community

santhoshkumar-io
santhoshkumar-io

Posted on

JS Function and its Types

Hi, Today I learned about function and its types in Javascript and thought of sharing it online.

Function Declaration

function textUp(){
  console.log("This is text up")
}
textUp(); //Function has been called
textUp(); //Function has been called
Enter fullscreen mode Exit fullscreen mode

Function with Arguments

Functions are used to perform a single operation to multiple variables that can be passed as argument respectively.

//Functions with different arguments passed on

 function sum(a:any,b:any){
  console.log( "The Sum is" a + b)
}

sum(5,4); //sum function called with 5 and 4 arguments passed on where it returns 9
sum(25,11); //sum function called with 25 and 11 arguments passed on where it returns 36
Enter fullscreen mode Exit fullscreen mode

Function Expression

The function can be defined with the name followed by const keyword as expression.

//Function Expression

const speak = function(){ 
  console.log("Have a good day")
}
Enter fullscreen mode Exit fullscreen mode

Function with Return Values

The return value will be retrieved when the function is called.

//Should return the solution when the function is called
const multiply = function(){
    return "The solution is " +  a*b;
}
Enter fullscreen mode Exit fullscreen mode

Arrow Function

The arrow function is the minimal form of the normal function and it makes the code cleaner.

//Function Expression
const calcArea = function (radius){
     return 3.15 * radius ** 2; //Returns the area
}
calArea(5);

//Arrow Function
const calcArea = (radius)=>{
     return 3.15 * radius ** 2;
}
calArea(5);//Returns the Area
Enter fullscreen mode Exit fullscreen mode

Thanks for reading!

Follow me for more content like this.

Cheers!

Top comments (0)