DEV Community

subash
subash

Posted on • Edited on

Types of Functions Made Easy – Full Guide for Beginners

there are 14 types of functions

Function Declaration
Function Expression
Arrow Function
Anonymous Function
Named Function Expression
IIFE
Generator Function
Async Function
Async Arrow Function
Method
Constructor Function
Class Method
Getter
Setter

1.Function Declaration

example;
function greet() {
console.log("Hello, World!");
}

greet();

In this case normal function this have function key word and
greet() is the function name or function identifier braces and curly braces also avail in this funtions .

2.Function Expression

example;

const greet = function() {
console.log("Hello!");
};

greet();

Function Expression means we should not put function name or identifier with function key word, we create variable then we put that function into that variable. that variable will consider that function name or identifier.

3.Arrow Function

const greet = () => {
console.log("Hello!");
};

greet();

This is arrow function have not function keyword and function name only start with braces like () , we can able to put this function in variable

common things in function expression and arrow function
*Are stored in variables
*Can be passed as arguments (callbacks)
*Are not hoisted like declarations

what is big diff between function expression and arrow function

TBD

TBD

In function declaration we can able to call the function name before enter function , but in function expression and arrow function is not working while use hoisted;
example:
console.log(subash(1,5));

function subash(c,s){
return c+s;
};
This program is only for declaration not suitable for arrow function and function expression.

Lambda Expression

let subash = (n1,n3) => n1%n3;

console.log(subash(3,3));

In this case function have not any name but return the n1%n3, we put that in one variable , we call the nameless function through the variable subash(3,3) , in this case we gave arguments successfully,
it will print in console as 1.

If we put curly braces{} then we have to mention the word return to return the n1%n3 , else no need to mention.

There is no term like lambda expression in javascript , it is only the concept used to create short line of program. this concept is applied in
Arrow and Anonymous function.

Arrow Function;
const add = (a, b) => a + b;

Anonymous function;
setTimeout(function() {
console.log("Hi");
}, 1000); #TBD

4.Anonymous function

reference website

  1. https://www.geeksforgeeks.org/javascript/functions-in-javascript/ 2.

Top comments (0)