JS Function and its Types

santhoshkumar

santhoshkumar-io

Posted on November 16, 2022

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!

💖 💪 🙅 🚩
santhoshkumar
santhoshkumar-io

Posted on November 16, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Callback Hell and Promises
javascript Callback Hell and Promises

April 3, 2023

JS Function and its Types
javascript JS Function and its Types

November 16, 2022

Maps in JavaScript
javascript Maps in JavaScript

August 3, 2022

JavaScript - Destructuring assignment
javascript JavaScript - Destructuring assignment

June 18, 2022